PFranchise
PFranchise

Reputation: 6752

Issues with reading from excel CSV and writing to another

I have tried a few things and have run into different errors with each attempt. First, I was reading and writing with the 'r' and 'w' options, but this lead to resulting csv having blank rows between actual rows when viewed in excel.

So, I figured out that I had to read and write with 'rb' and 'wb'. But, now I am getting the error: _csv.Error iterator should return strings, not bytes (did you open the file in text mode?).

Here is my code:

def readLines():
    r = csv.reader(open('test.csv', "rb"), dialect="excel")
    return [l for l in r] #causes the error

def writeFile(lines):
    resultFile = open('output.csv', 'wb')
    wr = csv.writer(resultFile, dialect='excel')
    wr.writerows(lines)

I do some altering of the lines object that requires they be strings. Am I going about this the correct way?

Upvotes: 1

Views: 6592

Answers (3)

user1008139
user1008139

Reputation: 301

If your text has accents or is in some language other than English you probably need to select the correct encoding from the Python Library standard. Some text editors allow you to know the encoding for a specific file. I found out with TextWrangler. So the correct Python code to read a file with accents (in Spanish), is:

import csv
def imp_csv(ruta):
    with open(ruta,'rt', newline='', encoding="mac_roman") as csvfile:
        r = csv.reader(csvfile, dialect='excel')
        for row in r:
            print(','.join(row))

Upvotes: 1

PFranchise
PFranchise

Reputation: 6752

The issue was that I was not setting the new line attribute.

Here is my updated code that is now working:

def readLines():
    r = csv.reader(open('test.csv', "rt", newline=''), dialect="excel")
    return [l for l in r]

def writeFile(lines):
    resultFile = open('output.csv', 'wt', newline='')
    wr = csv.writer(resultFile, dialect='excel')
    wr.writerows(lines)

Upvotes: 2

ben_frankly
ben_frankly

Reputation: 9940

Try opening the files in text mode:

r = csv.reader(open('test.csv', "rt"), dialect="excel")

resultFile = open('output.csv', 'wt')

Upvotes: 0

Related Questions