-1

I am trying to read a text file which contains a number of columns and dump the array into a dictionary. The first row would be a key of the dictionary, and the rest of the rows are list (values of the key), in which the header has : separation Data is from a text file not from a csv

DateTime:  Column1: test result: bar date:
 1-2-2012    a replicate    postive   1-3-2012 

Expected output:

{'DateTime':'1-2-2012','Column1':'a replicate','test result':'postive','bar date':'1-3-2012'}

1 Answers1

0

if your data is tab delimited you can use this approach

  1. get keys from first record
  2. get values from matching position in second record

if it's really csv delimited then split() using comma

data = '''DateTime:\tColumn1:\ttest result:\tbar date:
1-2-2012\ta replicate\tpostive\t1-3-2012'''
{t:data.split("\n")[1].split("\t")[i] for i, t in enumerate(data.split("\n")[0].split("\t"))}
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30