Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Writing a string with comma in one column of CSV file

21 views
Skip to first unread message

Mahmood Naderan

unread,
Jan 15, 2022, 3:56:47 PM1/15/22
to
Hi,
I use the following line to write some information to a CSV file which is comma delimited.

f = open(output_file, 'w', newline='')
wr = csv.writer(f)
...
f.write(str(n) + "," + str(key) + "\n" )


Problem is that key is a string which may contain ',' and this causes the final CSV file to have more than 2 columns, while I want to write the whole key as a single column.

I know that wr.writerow([key]) writes the entire key in one column, but I would like to do the same with write(). Any idea to fix that?


Regards,
Mahmood

alister

unread,
Jan 15, 2022, 4:00:25 PM1/15/22
to
you need to quote the data
the easies way to ensure this is to inculde to QUOTE_ALL option when
opening the file

wr = csv.writer(output, quoting=csv.QUOTE_ALL)



--
Chocolate chip.

dn

unread,
Jan 15, 2022, 4:11:52 PM1/15/22
to
On 16/01/2022 09.56, Mahmood Naderan via Python-list wrote:
> Hi,
> I use the following line to write some information to a CSV file which is comma delimited.
>
> f = open(output_file, 'w', newline='')
> wr = csv.writer(f)
> ...
> f.write(str(n) + "," + str(key) + "\n" )
>
>
> Problem is that key is a string which may contain ',' and this causes the final CSV file to have more than 2 columns, while I want to write the whole key as a single column.
>
> I know that wr.writerow([key]) writes the entire key in one column, but I would like to do the same with write(). Any idea to fix that?


This is 'CSV' rather than 'Python'. The solution is to "escape" the
string. Ref:
https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file

--
Regards,
=dn

Mahmood Naderan

unread,
Jan 15, 2022, 4:37:56 PM1/15/22
to
Right. I was also able to put all columns in a string and then use writerow().
Thanks.



Regards,
Mahmood
--
https://mail.python.org/mailman/listinfo/python-list

Avi Gross

unread,
Jan 15, 2022, 4:41:09 PM1/15/22
to
Mahmood,
Ask yourself WHY you want to do what you are doing. Are you using the power and features of the language or trying to do it step by step in the way that earlier languages often made you do it?
Yes, there are ways to include commas in fields of a CSV file and they can lead to complications you can completely avoid by NOT using a CSV file. Use something where commas are not a field separator and something else that is not likely to be in your data (or when found is removed or perhaps replaced with something like a space) and can thus be used.
Many files use a TAB or other symbols to delimit data and may be saved with names like a .TSV file or others. Python can trivially read in such files often simply by telling it to read something like a CSV but specifying the field separator is a tab or some other character.
But I have another dumb question. Why are you writing your CSV file by hand and a line at a time? I mean there is nothing wrong with that but many people have programs where they make an object like a DataFrame and manipulate that to have all the data they need and assuming we call the structure df, and they are using the pandas module, they can write the entire thing out like this:

df.to_csv('new_file.sv', sep='\t', index=False)

The pandas package fairly easily allows you to load in all the data you need rom an external source, search in what you have, make changes or additions, and write it out to many kinds of files.
Just a thought. If you like your way, fine, I see another reply suggesting how to hide the commas but that can be a problem if humans read and edit the results in the external file and do not follow through.

-----Original Message-----
From: Mahmood Naderan via Python-list <pytho...@python.org>
To: DL Neil via Python-list <pytho...@python.org>
Sent: Sat, Jan 15, 2022 3:56 pm
Subject: Writing a string with comma in one column of CSV file

Hi,
I use the following line to write some information to a CSV file which is comma delimited.

f = open(output_file, 'w', newline='')
wr = csv.writer(f)
...
f.write(str(n) + "," + str(key) + "\n" )


Problem is that key is a string which may contain ',' and this causes the final CSV file to have more than 2 columns, while I want to write the whole key as a single column.

I know that wr.writerow([key]) writes the entire key in one column, but I would like to do the same with write(). Any idea to fix that?


Regards,
Mahmood
--
https://mail.python.org/mailman/listinfo/python-list

Mats Wichmann

unread,
Jan 16, 2022, 10:19:00 AM1/16/22
to
On 1/15/22 13:56, Mahmood Naderan via Python-list wrote:
> Hi,
> I use the following line to write some information to a CSV file which is comma delimited.
>
> f = open(output_file, 'w', newline='')
> wr = csv.writer(f)
> ...
> f.write(str(n) + "," + str(key) + "\n" )
>
>
> Problem is that key is a string which may contain ',' and this causes the final CSV file to have more than 2 columns, while I want to write the whole key as a single column.

One of the reasons csv is a horrible data interchange format.

If you must... the convention for Excel, which is usually the reason
people are using csv, is you can enclose the entire comma-containing
field in "quote marks" (afaik it must be double-quote). The convention
for other consumers of csv may or may not accept this - if it's not for
Excel you'll need to check. One gotcha - the opening quote must appear
immediately after the preceding comma separator, you can't pad with
spaces. That is:

"this","should","be","okay, I think"

and not:

"this", "will", "not", "work", "correctly, I fear"


(the single-word row entries don't have to be in quotes, of course)

If you use the Python csv module it should take care of this - you can
specify the separator sequence and the quote sequence, and it knows to
quote a field correctly if it contains the separator. Highly recommended.

Greg Ewing

unread,
Jan 16, 2022, 5:38:29 PM1/16/22
to
On 17/01/22 4:18 am, Mats Wichmann wrote:
> the convention for Excel, which is usually the reason
> people are using csv, is you can enclose the entire comma-containing
> field in "quote marks" (afaik it must be double-quote).

And to include a double quote in a field, quote the field and
double the double-quote.

Another quirk is that Excel allows newlines in a quoted field,
which causes problems for naive parsers that split the input
into lines first and then analyse it into fields. (A particular
non-Python one I use professionally is guilty of this...)

--
Greg
0 new messages