Hi - 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 elsewhere in the codebase, I need to use a file object using pkg_resources.resource_stream
, as follows:
fileobj = pkg_resources.resource_stream('foo', 'tests/bar.csv')
reader = csv.reader(fileobj, dialect='excel')
header = reader.next()
(I'm simplifying here - basically the csv.reader
code is in a function over which I don't have control, and it expects a fileobj
.)
This throws 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 my fileobj
? I can't see anything about this in the pkg_resources
documentation.
Thanks.