CSV module - 'for row in reader' - between Mac and Windows

10,507

In Python 2

You need to open the file as a binary file:

csvfile = open('file.csv', 'rb')
csvreader = csv.reader(csvfile, delimiter = ',')
for row in csvreader:
    thisVariable = row[0]

http://docs.python.org/2/library/csv.html#csv.reader


In Python 3

You need to set newline='' in your open statement:

csvfile = open('file.csv', 'r', newline='')
csvreader = csv.reader(csvfile, delimiter = ',')
for row in csvreader:
    thisVariable = row[0]

http://docs.python.org/3.3/library/csv.html#csv.reader

Share:
10,507

Related videos on Youtube

DMML
Author by

DMML

Updated on August 12, 2022

Comments

  • DMML
    DMML over 1 year

    I developed some code on my Mac, using Wing IDE. The code i developed, which makes use of the csv module, is working, and does what I want it to on my Mac. The problem, though, is that the person I wrote it for needs to use it on Windows. I wasn't concerned about the code, as I'm not using any escape characters.

    The code looks like this:

    csvfile = open('file.csv', 'r')
    csvreader = csv.reader(csvfile, delimiter = ',')
    for row in csvreader:
       thisVariable = row[0]  # <<<--------
    

    The 'arrow' I put in above is where the error is returned at, on the Windows machine. Like I said, the code works fine on the Mac and, actually, this is pretty far down in the code that I have written. There are other CSV files read from and written to above this statement, which use similar indexing.

    I would really appreciate any ideas anybody might have regarding this issue! Thanks!

    • Danica
      Danica about 11 years
      Note that the people talking about binary mode are correct for Python 2, and @shellster's advice about newline='' (and a text-mode file) is correct for Python 3. This is probably the cause of the error, but it'd be very useful in determining that if you posted the actual text/traceback of the error message.
  • DMML
    DMML about 11 years
    Thank you, everyone, for all of the suggestions and advice. I plan on implementing the 'test' for the bad data (using the if not row- continue). The problem was with the escape character difference between Windows and Mac (linux)- \r\n and \n. The solution was to, each time a file is read from or created and written to, include a "newline = ''" specification: open(filename, 'r', newline = '')! Thanks again, everyone!