Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading