python - Elegant conversion of tab separated log lines into a CSV matrix -


i have tab-separated log file lines that:

event_a info1 info2 event_b info1 info4 event_c info2 info5 ... 

i want convert csv (with python) column has meaning. info1 e.g. id, info2 desintation, info4 coordinate etc.

i can "sloppy" way:

if "event_a" in line:    columns = line.split()    id = columns[1] 

i prefer have lookup matrix or table structure this, positions columns separated programming logic. there allows me define expressive overview here, code like:

for line in csvfile:     matches = event_table.match(line)     match in matches:        event_table.convert(match)         # add commas in between values account empty columns 

i of course create class per event, seems overkill. looking central definition, can use.

the output is:

 event_a, info1 info2  event_b, info1, , , info4  event_b, , info2, , , info5  ...  event_365, , , , , , , , , , , , info12  

edit: log file contains 100s of such event types, make code rather long , hard maintain doing "waterfall"-parsing way

it sounds split line first, , use cell[0] access lookup table

here's 1 way it, although feel i'm playing code golf.

lines = ['fooevent\tfoo_info1\tfoo_info2',      'barevent\tbar_info2\tbar_info4',      'basevent\tbas_info2\tbas_info5'      ]  mapper = {     'fooevent':(1,2),     'barevent':(2,4),     'basevent':(2,5) }  output_length = max([max(v) v in mapper.values()]) + 1  = lambda x,m: fields[1:][m.index(x)] if x in m else ''  line in lines:      fields = line.split('\t')     data = [a(x,mapper[fields[0]]) x in range(1,output_length)]     data.insert(0,fields[0])     print(','.join(data)) 

it might easier use hash individual mappers tuples, if because they're easier map other data types later if map table gets more complicated.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -