How to write output file in CSV format in python?

前端 未结 4 1863
忘了有多久
忘了有多久 2021-01-07 04:11

I tried to write output file as a CSV file but getting either an error or not the expected result. I am using Python 3.5.2 and 2.7 also.

Getting error in Python 3.5:

相关标签:
4条回答
  • 2021-01-07 04:24

    You are opening the input file in normal read mode but the output file is opened in binary mode, correct way

    resultFile = open("out.csv", "w")
    

    As shown above if you replace "wb" with "w" it will work.

    0 讨论(0)
  • 2021-01-07 04:36

    Others have answered that you should open the output file in text mode when using Python 3, i.e.

    with open('out.csv', 'w', newline='') as resultFile:
        ...
    

    But you also need to parse the incoming CSV data. As it is your code reads each line of the input CSV file as a single string. Then, without splitting that line into its constituent fields, it passes the string to the CSV writer. As a result, the csv.writer will treat the string as a sequence and output each character , including any terminating new line character, as a separate field. For example, if your input CSV file contains:

    1,2,3,4
    

    Your output file would be written like this:

    1,",",2,",",3,",",4,"
    "
    

    You should change the for loop to this:

    for row in csv.reader(f1):
        # process the row
        wr.writerow(row)
    

    Now the input CSV file will be parsed into fields and row will contain a list of strings - one for each field. For the previous example, row would be:

    for row in csv.reader(f1):
        print(row)
    
    ['1', '2', '3', '4']
    

    And when that list is passed to the csv.writer the output to the file will be:

    1,2,3,4
    

    Putting all of that together you get this code:

    import csv
    
    with open('input_1.csv') as f1, open('out.csv', 'w', newline='') as resultFile:
        wr = csv.writer(resultFile, dialect='excel')
        for row in csv.reader(f1):
            wr.writerow(row)
    
    0 讨论(0)
  • 2021-01-07 04:37

    open file without b mode

    b mode open your file as binary

    you can open file as w

    open_file = open("filename.csv", "w")
    
    0 讨论(0)
  • 2021-01-07 04:42

    On Python 3, csv requires that you open the file in text mode, not binary mode. Drop the b from your file mode. You should really use newline='' too:

    resultFile = open("out.csv", "w", newline='')
    

    Better still, use the file object as a context manager to ensure it is closed automatically:

    with open("input_1.csv", "r") as f1, \
         open("out.csv", "w", newline='') as resultFile:
        wr = csv.writer(resultFile, dialect='excel')
        for var in f1:
            wr.writerow([var.rstrip('\n')])
    

    I've also stripped the lines from f1 (just to remove the newline) and put the line in a list; csv.writer.writerow wants a sequence with columns, not a single string.

    Quoting the csv.writer() documentation:

    If csvfile is a file object, it should be opened with newline='' [1]. [...] All other non-string data are stringified with str() before being written.

    [1] If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

    0 讨论(0)
提交回复
热议问题