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

csv, indexing, macos, python, windows

Solution

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

Problem

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!

Original source