Open file in universal-newline mode using pkg_resources?

I am processing a CSV file and have the following working code:

reader = csv.reader(open(filename, 'rU'), dialect='excel')
header = reader.next()

However, to be compatible with other places in the code base, I need to use a file object using pkg_resources.resource_stream, as shown below:

fileobj = pkg_resources.resource_stream('foo', 'tests/bar.csv')
reader = csv.reader(fileobj, dialect='excel')
header = reader.next()

(I simplify here - basically the code csv.readeris in a function that I have no control over, and it expects fileobj.)

This causes the following error.

Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Any idea how I can use universal-newline mode with mine fileobj? I do not see anything about this in the documentation pkg_resources.

Thank.

+3
source share
1 answer

fd (, ), os.fdopen(fileobj.fileno(), 'rU'), .

+6

Source: https://habr.com/ru/post/1772122/


All Articles