4

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
  • 963,270
  • 265
  • 3,804
  • 3,187
MSY
  • 43
  • 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

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

print mydict
nosklo
  • 205,639
  • 55
  • 286
  • 290