-1

i have a text file which looks like this sample.txt

49416 286:25:58 2570460 36252408 04:29:00 R qp256
49486 180:56:21 5714784 7585688 06:44:33 R qp32
49501 58:19:52 36640572 39860816 02:02:09 R qp32

how can i get an output in the form of dictionary or assign it to a new file or a list(which ever is better method) so that i can use the output later and access each of those elements . I found an example which does something like this but could not match it to my code

newdict:

{'j_id':'49416','t1':'286:25:58','t2':'2570460','t3':'36252408','ot':'04:29:00','stat':'R','q':'qp256'}
{'j_id':'49486','t1':'180:56:21','t2':'5714784','t3':'7585688','ot':'06:44:33','stat':'R','q':'qp32'}
{'j_id':'49501','t1':'58:19:52','t2':'36640572','t3':'39860816','ot':'02:02:09','stat':'R','q':'qp32'}
Community
  • 1
  • 1
sharath
  • 11
  • 5

1 Answers1

0

The straightforward way would be:

keys = ['j_id', 't1', 't2', 't3', 'ot', 'stat', 'q']
dicts = []
with open('sample.txt') as f:
    for line in f:
        values = line.strip().split()
        dicts.append(dict(zip(keys, values)))
print dicts

If you need something more robust use the csv module.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88