87

I am trying to access a model.filefield in Django to parse a CSV file in Python using the csv module. It's working on Windows, but on Mac it gave me this:

Exception Type: Error

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

This is the code:

myfile = customerbulk.objects.all()[0].fileup

mydata = csv.reader(myfile)
    for email,mobile,name,civilid in mydata:
        print email,mobile,name,civilid
4
  • what is this customerbulk.objects.all()[0].fileup thing. Is it a filename on a model? Commented Jul 17, 2011 at 21:42
  • fileup = models.FileField(verbose_name="CsvFile",upload_to='ExcelFiles') if i make a small query like customerbulk.objects.get(pk=1) Commented Jul 17, 2011 at 21:49
  • 1
    The exact reason of using rU (It's related to the open() function and is not csv specific.): In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newlines support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'. docs.python.org/2/library/functions.html#open Commented Jun 4, 2013 at 2:53
  • In Python >= 3, use newline='' instead of mode='U'. Commented Jan 19, 2018 at 14:37

1 Answer 1

151

I finally found the solution:

mypath = customerbulk.objects.get(pk=1).fileup.path
o = open(mypath,'rU')
mydata = csv.reader(o)
Sign up to request clarification or add additional context in comments.

5 Comments

I believe you can simplify this. It seems odd to seek to the start after just opening the file. The following helped me overcome the same issue from an Excel spreadsheet export to CSV using the defaults: data = csv.reader(open(FILENAME, 'rU'), quotechar='"', delimiter = ',')
Yes, there's no need to seek to beginning of file just after opening. The >>>o = open(mypath, 'rU') bit, with the 'rU' flag is the important magic that worked in my case.
PEP278 explained what rU stands for:In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". Mode "rU" is also allowed, for symmetry with "rb".
@Xiao +1 for linking to the original PEP which explains the rationale.
In Python >= 3, use newline instead, the default is newline=None which is like newline='' except it also translates the newlines to \n. I'm unsure which of these is equivalent to mode='U'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.