user9599919
user9599919

Reputation: 63

Convert multiple line on a file to tuple & lists format

I am trying to covert the multiple lines under a file, to below format (tuples + list), but still struggling

sample lines under file

USER1.TEST1,SCHEMA2.TEST2
USER5.TEST,USER3.TEST1,RATE=100
SCHEMA5.TEST5,CORE12.TEST3,RATE=500

Expected output

[('USER','TEST1','USER','TEST2'),('USER5','TEST','USER3','TEST1','RATE=100'),('SCHEMA5','TEST5','CORE12','TEST3','RATE=500')]

Code i am trying ...

o_list = []
with open (i_list,'rb') as f:
    if not 'tab' in i_list:
        r = csv.reader(f)
    else:
        for line in f.readlines():
            f, s = line.strip().split('.')
            s = s.split(',')
            o_list.append((f,) + tuple(s))

        return o_list

Upvotes: 0

Views: 305

Answers (2)

Mark
Mark

Reputation: 92471

It seems like as long as you are using the csv module, you should leverage the fact that it will split on a delimiter of your choice. So you can split on , and then on each line split the fields if you need to. You can use itertools.chain to flatten the sublists into tuples:

from itertools import chain

with open ('test.csv') as f:
    if not 'tab' in i_list: # not sure what i_list is
        r = csv.reader(f)
    else:
        r = csv.reader(f, delimiter=',') # split on `,` first
        o_list = [tuple(chain.from_iterable(s.split('.') for s in line)) for line in r]

o_list:

[('USER1', 'TEST1', 'SCHEMA2', 'TEST2'),
 ('USER5', 'TEST', 'USER3', 'TEST1', 'RATE=100'),
 ('SCHEMA5', 'TEST5', 'CORE12', 'TEST3', 'RATE=500')]

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71620

Try using a one-liner list comprehension with a re.split:

import re
with open('filename.txt', 'r') as f:
    print([re.split('\.|,', i.rstrip()) for i in f])

Output:

[['USER1', 'TEST1', 'SCHEMA2', 'TEST2'], ['USER5', 'TEST', 'USER3', 'TEST1', 'RATE=100'], ['SCHEMA5', 'TEST5', 'CORE12', 'TEST3', 'RATE=500']]

Upvotes: 2

Related Questions