0

I'm receiving a CSV file via an upload that I would like to parse, but I get Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? when I try to open it via this line of code:

reader = csv.reader(args['file'], quotechar='|', dialect=csv.excel)

Do I need to save the file temporarily, then open it in universal-newline mode to achieve this? Or is there a way to do so without having to use a temp file?

Aaron
  • 2,367
  • 3
  • 25
  • 33
  • That's what I'd try as far as I'm concerned - if only to make sure the problem comes from this. – bruno desthuilliers Oct 24 '14 at 14:06
  • @brunodesthuilliers I resolved it by changing my code to have the file argument be `args['file'].read().splitlines()`. This resolves the newline issue because I just split the file into an array of strings which the `reader` object handles nicely. – Aaron Oct 24 '14 at 14:45
  • I'm not sure your "solution" is safe (in fact I'm quite sure it's _not_ safe) - there's a reason for the error message you got, and you might end up with garbled data. My 2 cents... – bruno desthuilliers Oct 24 '14 at 14:51
  • @brunodesthuilliers check out [this](http://stackoverflow.com/questions/5341174/upload-and-parse-csv-file-with-universal-newline-in-python-on-google-app-engin) and let me know what you think – Aaron Oct 24 '14 at 15:19
  • your question has been closed as duplicate of http://stackoverflow.com/questions/1875956/how-can-i-access-an-uploaded-file-in-universal-newline-mode - you probably want to have a read ? – bruno desthuilliers Oct 24 '14 at 15:27

1 Answers1

0

It seems that you don't need to save it since it returns a reader object where you can iterate on it (just think of working with files).

Dataman
  • 3,457
  • 3
  • 19
  • 31
  • I know that, but my issue is that it can't read it because of the newline error. My logic behind saving it is so that I could re-open it in universal newline mode – Aaron Oct 24 '14 at 13:55