views:

9

answers:

1

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.

A: 

If the stream always has an fd (e.g. because it's a normal opened file on the filesystem), you can use os.fdopen(fileobj.fileno(), 'rU') to open it with the right mode.

adw