使用python将带有逗号的文本写入CSV文件的单元格中

发布于 2024-09-14 01:46:08 字数 135 浏览 3 评论 0原文

我想将带有逗号的文本写入 CSV 文件的单元格中。

输入

'1,2,3,Hello'

CSV 中的输出应为

'1,2,3','Hello'

I want to write text with comma into a cell in CSV file.

Input

'1,2,3,Hello'

Output in CSV should be

'1,2,3','Hello'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知在何时 2024-09-21 01:46:08

使用正确的 CSV writer

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'))
>>> spamWriter.writerow(['Spam', 'Lovely, Spam'])

输出:

垃圾邮件,“可爱,垃圾邮件”

Use the proper CSV writers:

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'))
>>> spamWriter.writerow(['Spam', 'Lovely, Spam'])

Outputs:

Spam,"Lovely, Spam"

ぇ气 2024-09-21 01:46:08

这不是 Python 特有的,而是与 CSV“标准” 有关。

如果您想将控制字符写入作为值的一部分,则需要通过用双引号将其括起来来转义该值:

f.write('1,2,3,45,"The Next comma I want to write and not separate to another cell, so this sentence will be whole",6,7,8')

编辑:尽管在实践中,最好按照建议使用 CSV 编写器接口由其他人。当有一个现成的库可以为您抽象出实现细节时,让自己陷入实现细节从来都不是一个好主意。

This is not Python specific, but is to do with the CSV "standard".

If you want to write a control character as part of your value, you'll need to escape the value by surrounding it in double-quotes:

f.write('1,2,3,45,"The Next comma I want to write and not separate to another cell, so this sentence will be whole",6,7,8')

Edit: Although in practice, it will be a better idea to use the CSV writer interfaces as suggested by others. It's never a good idea to embroil yourself in the implementation details when there's a ready-rolled library that abstracts this away for you.

橙幽之幻 2024-09-21 01:46:08

文件的每一行都是一条数据记录。每条记录由一个或多个字段组成,以逗号分隔。

用逗号分隔字段的基本思想很清楚,但是当字段数据也可能包含逗号甚至嵌入换行符时,这个思想就会变得复杂。

CSV 实现可能无法处理此类字段数据,或者可能使用引号将字段括起来。 -- 来自 https://en.wikipedia.org/wiki/Comma-separated_values

因此,您可以使用引号将每个字段文本括起来。

假设输入为 ['1,2,3', 'Hello'],则 CSV 的输出应为 "1,2,3", "Hello",您可以使用下面的代码来实现此目的。

>>> ",".join('"{0}"'.format(s) for s in ['1,2,3', 'Hello'])
'"1,2,3","Hello"'

但是当文本中存在一些特殊符号,如 "\n 等时,你就会遇到问题。python

csv 库已经为你处理了所有的边缘情况。

编写 写入

可以使用 @Dominic Rodger 答案

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'))
>>> spamWriter.writerow(['Spam', 'Lovely, Spam'])

字符串

https://stackoverflow.com/a/9157370/5238892 Python

在 Python 3 中:

>>> import io
>>> import csv
>>> output = io.StringIO()
>>> csvdata = [1,2,'a','He said "what do you mean?"',"Whoa!\nNewlines!"]
>>> writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
>>> writer.writerow(csvdata)
59
>>> output.getvalue()
'1,2,"a","He said ""what do you mean?""","Whoa!\nNewlines!"\r\n'

2 需要稍微更改一些细节:

>>> output = io.BytesIO()
>>> writer = csv.writer(output)
>>> writer.writerow(csvdata)
57L
>>> output.getvalue()
'1,2,a,"He said ""what do you mean?""","Whoa!\nNewlines!"\r\n'

Each line of the file is a data record. Each record consists of one or more fields, separated by commas.

The basic idea of separating fields with a comma is clear, but that idea gets complicated when the field data may also contain commas or even embedded line-breaks.

CSV implementations may not handle such field data, or they may use quotation marks to surround the field. -- From https://en.wikipedia.org/wiki/Comma-separated_values

So you can use quotation marks to surround the each field text.

Suppose the input is ['1,2,3', 'Hello'], the output to CSV should be "1,2,3", "Hello", you can use codes below to achieve this.

>>> ",".join('"{0}"'.format(s) for s in ['1,2,3', 'Hello'])
'"1,2,3","Hello"'

But you will encounter problems when there are some special symbols such as ", \n and etc in text.

The python csv library has handled all the edge cases for you.

Write to file

Can use @Dominic Rodger answer.

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'))
>>> spamWriter.writerow(['Spam', 'Lovely, Spam'])

Write to string

From https://stackoverflow.com/a/9157370/5238892.

In Python 3:

>>> import io
>>> import csv
>>> output = io.StringIO()
>>> csvdata = [1,2,'a','He said "what do you mean?"',"Whoa!\nNewlines!"]
>>> writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
>>> writer.writerow(csvdata)
59
>>> output.getvalue()
'1,2,"a","He said ""what do you mean?""","Whoa!\nNewlines!"\r\n'

Some details need to be changed a bit for Python 2:

>>> output = io.BytesIO()
>>> writer = csv.writer(output)
>>> writer.writerow(csvdata)
57L
>>> output.getvalue()
'1,2,a,"He said ""what do you mean?""","Whoa!\nNewlines!"\r\n'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文