5

I have a.csv file as below:

 OLD,NEW
 AA,XX
 BB,YY
 CC,ZZ

I wanna convert it to dict format, say dict1 = {'AA':'XX','BB':'YY','CC':'ZZ'} Should I use DictReader or join string?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MSY
  • 53
  • 1
  • 3
  • Just two columns? Ignoring the first line? I guess [this solution](http://stackoverflow.com/questions/4356329/creating-a-python-dictionary-from-a-line-of-text) is overkill. – johnsyweb Jun 01 '11 at 21:19

1 Answers1

13
with open('file.csv') as f:
    f.readline() # ignore first line (header)
    mydict = dict(csv.reader(f, delimiter=','))

print mydict
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 1
    @MSY: "Thanks [...] it solved my question" is best expressed [by clicking on the check box outline to the left of the answer](http://stackoverflow.com/faq#howtoask). – johnsyweb Jun 01 '11 at 22:51
  • Ok, checked. Appreciate this. – MSY Jun 02 '11 at 21:47