2

I have the need to deal using cvs.reader objects because of the order of the columnts must be preserved. But, I would like also to have the csv.DictReader object for some other reasons.

Now, is there a simple posibility to convert them between each other without having re-reading the file?

Bingo
  • 83
  • 4
  • 1
    AFAIK `DictWriter` *does* preserve the order in which columns are written, so I don't understand what you are asking. Can you provide an example code that you would like to use, its expected output and the output you get instead? – Bakuriu Nov 03 '13 at 16:26

1 Answers1

0

I'd recommend you to use tablib instead.

ds = tablib.Dataset()
ds.csv = open(csvfile).read()

Then you can iter over ds as a sequence of rows, and ds.dict gives you a list of OrderedDict instances for each row. It's much easier if you also need to get it to other formats, or edit and save again as csv.

Pedro Werneck
  • 40,902
  • 7
  • 64
  • 85