Writing with Python's built-in .csv module

前端 未结 3 1846
遥遥无期
遥遥无期 2021-02-03 14:20

[Please note that this is a different question from the already answered How to replace a column using Python’s built-in .csv writer module?]

I need to do a find and rep

3条回答
  •  野性不改
    2021-02-03 14:36

    Opening csv files as binary is just wrong. CSV are normal text files so You need to open them with

    source = open("PALTemplateData.csv","r")
    target = open("AnotherFile.csv","w")
    

    The error

    _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
    

    comes because You are opening them in binary mode.

    When I was opening excel csv's with python, I used something like:

    try:    # checking if file exists
        f = csv.reader(open(filepath, "r", encoding="cp1250"), delimiter=";", quotechar='"')
    except IOError:
        f = []
    
    for record in f:
        # do something with record
    

    and it worked rather fast (I was opening two about 10MB each csv files, though I did this with python 2.6, not the 3.0 version).

    There are few working modules for working with excel csv files from within python - pyExcelerator is one of them.

提交回复
热议问题