Skip to content Skip to sidebar Skip to footer

CSV Should Return Strings, Not Bytes Error

I am trying to read CSV files from a directory that is not in the same directory as my Python script. Additionally the CSV files are stored in ZIP folders that have the exact same

Solution 1:

It's a bit of a kludge and I'm sure there's a better way (that just happens to elude me right now). If you don't have embedded new lines, then you can use:

import zipfile, csv

zf = zipfile.ZipFile('testing.csv.zip')
with zf.open('testing.csv', 'r') as fin:
    # Create a generator of decoded lines for input to csv.reader
    # (the csv module is only really happy with ASCII input anyway...)
    lines = (line.decode('ascii') for line in fin)
    for row in csv.reader(lines):
        print(row)

Post a Comment for "CSV Should Return Strings, Not Bytes Error"