Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python write to csv with comas in each field

I'm trying to export a list of strings to csv using comma as a separator. Each of the fields may contain a comma. What I obtain after writing to a csv file is that every comma is treated as a separator.

My question is: is it possible to ignore the commas (as separators) in each field?

Here is what I'm trying, as an example.

import csv

outFile = "output.csv"
f = open(outFile, "w")

row = ['hello, world' , '43333' , '44141']

with open(outFile, 'w') as writeFile:
    writer = csv.writer(writeFile)
    writer.writerow(row)

writeFile.close()

The output looks like this: outputObtained

What I would like is something like this: outputExpected

I think a way to solve this would be to use a different separator, as I read in some sites. But my question is if there is a way to solve this using comma (',') separators.

Thanks

like image 993
Carlos Avatar asked Jan 25 '19 16:01

Carlos


People also ask

How do you read a CSV file with commas within a field in Python?

info = csv. reader(open('./info. csv')) for row in info : print row[0] + " * " + row[1] ... Is it possible to read double quoted fields which contains a comma?

How do you handle commas in data in a CSV file in Python?

A CSV file is a simple text file where each line contains a list of values (or fields) delimited by commas. Although the term "Comma" appears in the format name itself, but you will encounter CSV files where data is delimited using tab ( \t ) or pipe ( | ) or any other character that can be used as a delimiter.

What is Quotechar in CSV Python?

quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote ( ' " ' ). escapechar specifies the character used to escape the delimiter character, in case quotes aren't used.


Video Answer


1 Answers

You just need to tell Calc/Excel that your quoting character is a "

like image 57
DM Graves Avatar answered Oct 09 '22 06:10

DM Graves