pythonreader

How to access raw csv content in universal-newline mode using DictReader?


I have these lines of code:

>>> import csv, io
>>> raw_csv = io.StringIO('one,two,three\r1,2,3\r,1,2,3')
>>> reader = csv.DictReader(raw_csv, delimiter=',')
>>> list(reader)

which results the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.9/csv.py", line 110, in __next__
    self.fieldnames
  File ".../lib/python3.9/csv.py", line 97, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

I found solutions using with open() but I cannot use it since there is no file path. I also need to use DictReader.


Solution

  • You can pass newline='' to the StringIO constructor to enable universal newline mode:

    >>> reader = csv.DictReader(StringIO(file), delimiter=',')
    >>> next(reader)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.10/csv.py", line 110, in __next__
        self.fieldnames
      File "/usr/lib/python3.10/csv.py", line 97, in fieldnames
        self._fieldnames = next(self.reader)
    _csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
    
    >>> reader = csv.DictReader(StringIO(file, newline=''), delimiter=',')
    >>> next(reader)
    {'one': '1', 'two': '2', 'three': '3'}