Home:ALL Converter>How to write output file in CSV format in python?

How to write output file in CSV format in python?

Ask Time:2016-09-12T18:01:57         Author:krish

Json Formatter

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:

wr.writerow(var)
TypeError: a bytes-like object is required, not 'str'

and

In Python 2.7, I am getting all column result in one column.

Expected Result:
An output file same format as the input file.

Code:

import csv

f1 = open("input_1.csv", "r") 

resultFile = open("out.csv", "wb")
wr = csv.writer(resultFile, quotechar=',') 

def sort_duplicates(f1):
  for i in range(0, len(f1)):
      f1.insert(f1.index(f1[i])+1, f1[i])
      f1.pop(i+1)

for var in f1:
      #print (var)
      wr.writerow([var]) 

If I am using resultFile = open("out.csv", "w"), I get one row extra in the output file.

If I am using above code, getting one row and column extra.

Author:krish,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39447817/how-to-write-output-file-in-csv-format-in-python
Mikail Land :

open file without b mode\n\nb mode open your file as binary\n\nyou can open file as w\n\nopen_file = open(\"filename.csv\", \"w\")\n",
2016-09-12T10:06:41
Martijn Pieters :

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:\n\nresultFile = open(\"out.csv\", \"w\", newline='')\n\n\nBetter still, use the file object as a context manager to ensure it is closed automatically:\n\nwith open(\"input_1.csv\", \"r\") as f1, \\\n open(\"out.csv\", \"w\", newline='') as resultFile:\n wr = csv.writer(resultFile, dialect='excel')\n for var in f1:\n wr.writerow([var.rstrip('\\n')])\n\n\nI'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.\n\nQuoting the csv.writer() documentation:\n\n\n 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.\n \n [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.\n",
2016-09-12T10:04:56
mhawke :

Others have answered that you should open the output file in text mode when using Python 3, i.e.\n\nwith open('out.csv', 'w', newline='') as resultFile:\n ...\n\n\nBut 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:\n\n\n1,2,3,4\n\n\nYour output file would be written like this:\n\n\n1,\",\",2,\",\",3,\",\",4,\"\n\"\n\n\nYou should change the for loop to this:\n\nfor row in csv.reader(f1):\n # process the row\n wr.writerow(row)\n\n\nNow 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:\n\nfor row in csv.reader(f1):\n print(row)\n\n\n\n['1', '2', '3', '4']\n\n\nAnd when that list is passed to the csv.writer the output to the file will be:\n\n\n1,2,3,4\n\n\n\n\nPutting all of that together you get this code:\n\nimport csv\n\nwith open('input_1.csv') as f1, open('out.csv', 'w', newline='') as resultFile:\n wr = csv.writer(resultFile, dialect='excel')\n for row in csv.reader(f1):\n wr.writerow(row)\n",
2016-09-12T10:37:18
yy