1

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.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
light
  • 49
  • 5
  • 1
    Why does the string use `\r` instead of `\n`? This isn't really valid CSV any way you slice it. Did you actually mean for it to contain a header line and two data lines? What does the `with open` solution look like? – tripleee Apr 26 '22 at 08:25
  • I saw \r is an old Macintosh convention: https://docs.python.org/3/glossary.html#term-universal-newlines – light Apr 26 '22 at 08:31
  • The with open() solutions by using newline='' I added the link in the quesion – light Apr 26 '22 at 08:32

1 Answers1

3

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'}
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • The `csv` module [docs](https://docs.python.org/3/library/csv.html#csv.reader) say to open files with `newline=''`, it's nice to know that `StringIO` supports that. – martineau Apr 26 '22 at 08:42
  • Of course, with a static string, you could populate it with `\n` instead of `\r` in the first place, or (somewhat more expensively) `StringIO(file.replace('\r', '\n'))` – tripleee Apr 26 '22 at 08:43
  • 1
    @tripleee Of course, but I assume that the static string was only for the sake of making a minimal example :) – Thomas Apr 26 '22 at 09:25