How can I make OrderedDict from csv? Is there any function?
csv:
1 one
2 two
OrderedDict:
OrderedDict((('1', 'one'), ('2', 'two')))
If your csv has two columns as described in your question, you can do this:
import csv
import collections
with open('foo.csv','rb') as f:
r = csv.reader(f)
od = collections.OrderedDict(r)
If the rows in the csv file were formatted as key, value1, value2, value3
you would do this:
with open('foo.csv','rb') as f:
r = csv.reader(f)
od = collections.OrderedDict((row[0], row[1:]) for row in r)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With