有没有办法在不破坏格式的情况下在 CSV 列中包含逗号? [英] Is there a way to include commas in CSV columns without breaking the formatting?

查看:29
本文介绍了有没有办法在不破坏格式的情况下在 CSV 列中包含逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有名称和数字的两列 CSV.有些人的名字使用逗号,例如 Joe Blow,CFA. 这个逗号破坏了 CSV 格式,因为它被解释为一个新列.

I've got a two column CSV with a name and a number. Some people's name use commas, for example Joe Blow, CFA. This comma breaks the CSV format, since it's interpreted as a new column.

我已经阅读过,最常见的方法似乎是用一个新值(例如 this|that|the, other)替换那个字符,或者替换分隔符.

I've read up and the most common prescription seems to be replacing that character, or replacing the delimiter, with a new value (e.g. this|that|the, other).

我真的很想保留逗号分隔符(我知道 excel 支持其他分隔符,但其他解释器可能不支持).我还想在名称中保留逗号,如 Joe Blow|CFA 看起来很傻.

I'd really like to keep the comma separator (I know excel supports other delimiters but other interpreters may not). I'd also like to keep the comma in the name, as Joe Blow| CFA looks pretty silly.

有没有办法在不破坏格式的情况下在 CSV 列中包含逗号,例如通过转义它们?

Is there a way to include commas in CSV columns without breaking the formatting, for example by escaping them?

推荐答案

用引号将字段括起来,例如

Enclose the field in quotes, e.g.

field1_value,field2_value,"field 3,value",field4, etc...

请参阅维基百科.

更新:

对引号进行编码,使用",一个字段中的一个双引号将被编码为"",整个字段将变成"""".因此,如果您在 Excel 中看到以下内容:

To encode a quote, use ", one double quote symbol in a field will be encoded as "", and the whole field will become """". So if you see the following in e.g. Excel:

---------------------------------------
| regular_value |,,,"|  ,"", |"""   |"|
---------------------------------------

CSV 文件将包含:

regular_value,",,,""",","""",","""""""",""""

逗号只是用引号封装起来,所以,变成了",".

A comma is simply encapsulated using quotes, so , becomes ",".

逗号和引号需要封装和引用,所以","变成了""",""".

A comma and quote needs to be encapsulated and quoted, so "," becomes """,""".

这篇关于有没有办法在不破坏格式的情况下在 CSV 列中包含逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆