简体   繁体   English

CSV中的双“双引号”处理

[英]Dealing with double “double quotations” in CSV

I am processing some data stored in a CSV file with python.我正在使用 python 处理存储在 CSV 文件中的一些数据。 There was a line like this:有这样一行:

title, tags, views, like
"Hello, world!" in python, ['beginner', 'tutorial'], '550', '540'

As you can see, there are many commas in the data which can break the CSV file.如您所见,数据中有许多逗号会破坏 CSV 文件。 So, I enclosed all the elements with double quotations.所以,我用双引号将所有元素括起来。 But, there are already double quotations in the title.但是,标题中已经有双引号。 So, if I use a double quotation again, it just breaks the CSV file.所以,如果我再次使用双引号,它只会破坏 CSV 文件。 Which look like this:看起来像这样:

"Hello | World," in python | ['beginner', 'tutorial'] | 550 | 540

(Added " | " to make things clear) (添加“|”以使事情清楚)

And I want it to look like this:我希望它看起来像这样:

"Hello, world," in python | ['beginner', 'tutorial'] | 550 | 540

How can I fix this?我怎样才能解决这个问题?

Well, if you can't apply a delimiter to the commas that are not field separators...好吧,如果您不能对不是字段分隔符的逗号应用分隔符...

Can you export the data with a field separating delimiter that is not in the data?您可以使用不在数据中的字段分隔分隔符导出数据吗?

Alternately can you first convert all the commas that occur in the data to some odd character.. then export the data as a CSV file.或者,您可以先将数据中出现的所有逗号转换为一些奇怪的字符。然后将数据导出为 CSV 文件。

After importing the data into the database... or your application... run things through a function to convert the chosen odd character back to commas.将数据导入数据库后...或您的应用程序...通过 function 运行以将所选奇数字符转换回逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM