my text file format is:
apple healthy orange tangy , juicy banana yellow in color , yummy i need create either 2 lists:
l1 = ['apple','orange','banana'] l2=['very healthy','tangy , juicy','yellow in color , yummy'] or convert values dictionary:
d1={'apple':'very healthy','orange':'tangy , juicy','banana':'yellow in color , yummy'} the first 2 columns in file separated tab.
i tried following code change 2 lists , convert dictionary:
l1=[] l2=[] d={} read_file=open('edges.txt','r') split= [line.strip() line in read_file] line in split: l1.append(line.split('\t')[0]) l2.append(line.split('\t')[1:]) d=dict(zip(l1,l2)) print d i getting incorrect values. newbie python..
line.split('\t') returns list, , line.split('\t')[0] returns first element of list ('apple', 'orange', 'banana').
l2.append(line.split('\t')[1:] returns list because [1:] slice. maybe want l2.append(line.split('\t')[1] instead?
i couldn't resist rewriting code:
d={} line in open('edges.txt','r'): split = line.strip().split('\t', 1) d[split[0]] = split[1] print d
Comments
Post a Comment