生成 CSV 文件时避免双引号混淆?
Avoid double quote confusion when generating CSV files?
我正在将一些字符串推送到 CSV 文件中:
csv_string = CSV.generate({col_sep: ";", row_sep: "\n"}) do |csv|
csv << ["101-41", "Sparri, \"Violet\" (rod) (1 bunt á 10 stk.)"]
end
当 CSV 文件最终生成时,常规 Excel 安装显然会正确显示内容,但通过纯文本编辑器(如 Sublime Text)仔细查看会发现双引号造成混淆:
10-41;"Sparri, ""Violet"" (rod) (1 bunt á 10 stk.)"
我怎样才能避免这种情况?在推送到 CSV 文件之前,我应该将双引号转换成不同的内容吗?
这是正确的,一个双引号被另一个双引号转义了。来自 RFC 4180:
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
CSV 不是官方标准,尽管它很奇怪。
有 RFC 4180 guidelines 说明双引号
5. Each field may or may not be enclosed in double quotes (however
some programs, such as Microsoft Excel, do not use double quotes
at all). If fields are not enclosed with double quotes, then
double quotes may not appear inside the fields. For example:
"aaa","bbb","ccc" CRLF
zzz,yyy,xxx
6. Fields containing line breaks (CRLF), double quotes, and commas
should be enclosed in double-quotes. For example:
"aaa","b CRLF
bb","ccc" CRLF
zzz,yyy,xxx
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
所以你的格式是正确的,只要 Excel 正确显示文件,我就不会介意双“双引号”
我正在将一些字符串推送到 CSV 文件中:
csv_string = CSV.generate({col_sep: ";", row_sep: "\n"}) do |csv|
csv << ["101-41", "Sparri, \"Violet\" (rod) (1 bunt á 10 stk.)"]
end
当 CSV 文件最终生成时,常规 Excel 安装显然会正确显示内容,但通过纯文本编辑器(如 Sublime Text)仔细查看会发现双引号造成混淆:
10-41;"Sparri, ""Violet"" (rod) (1 bunt á 10 stk.)"
我怎样才能避免这种情况?在推送到 CSV 文件之前,我应该将双引号转换成不同的内容吗?
这是正确的,一个双引号被另一个双引号转义了。来自 RFC 4180:
If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:
"aaa","b""bb","ccc"
CSV 不是官方标准,尽管它很奇怪。 有 RFC 4180 guidelines 说明双引号
5. Each field may or may not be enclosed in double quotes (however
some programs, such as Microsoft Excel, do not use double quotes
at all). If fields are not enclosed with double quotes, then
double quotes may not appear inside the fields. For example:
"aaa","bbb","ccc" CRLF
zzz,yyy,xxx
6. Fields containing line breaks (CRLF), double quotes, and commas
should be enclosed in double-quotes. For example:
"aaa","b CRLF
bb","ccc" CRLF
zzz,yyy,xxx
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
所以你的格式是正确的,只要 Excel 正确显示文件,我就不会介意双“双引号”