正确使用CSV中的双引号

srg*_*rgb 139 csv escaping

我的CSV中有这样的一行:

"Samsung U600 24"","10000003409","1","10000003427"

旁边的引用24用于表示英寸,而该引号旁边的引号将关闭该字段.我正在阅读该行fgetcsv但解析器出错并将值读取为:

Samsung U600 24",10000003409"

我尝试在英寸引号前加一个反斜杠,但后来我只是在名字中得到一个反斜杠:

Samsung U600 24\"

有没有办法在CSV中正确地逃避这个,所以值是Samsung U600 24",或者我必须在处理器中正则它?

use*_*035 251

使用2个引号:

"Samsung U600 24"""
Run Code Online (Sandbox Code Playgroud)

  • RFC-4180,段落"如果双引号用于封闭字段,那么出现在字段内的双引号必须通过在其前面加上另一个双引号来转义." (86认同)
  • 如tommed所说,您只需要添加一个双引号即可转义双引号。您可以使用称为csvfix的命令行工具来检测任何不符合要求的行:csvfix check -nl -v [filename] (3认同)
  • 单双双单双引号是必需的,但前提是双单双引号...祝你好运! (3认同)
  • @SamCritchley我只看到一个双引号被用来逃离这里.通过"使用2引号",user4035表示1引号应替换为2引号.通过使用双引号转义双引号,您可以有效地创建双引号对(2个双引号).您最后看到的最终报价是终止该字段. (2认同)

Ang*_*dar 17

不仅是双引号,还需要单引号('),双引号("),反斜杠(\)和NUL(NULL字节).

使用fputcsv()写,fgetcsv()读,将采取一切照顾.

  • @Angelin Nadar,你能否为你的声明增加一个来源,说明需要加倍单引号,反斜杠和NUL?我没有在[RFC-4180](https://tools.ietf.org/html/rfc4180)中找到它. (14认同)
  • 为什么这个答案会被投票?关于转义字符的评论从未得到支持,原始问题也没有询问 PHP。当某个程序(例如 Open Office)允许您更改它时,这似乎仅适用于字符串定界符(并且仅适用于所选定界符)。 (9认同)
  • *如果您真正阅读过它* RFC4180 指出,**CSV 格式有各种[其他]规范和实现**,并列出了**至少 4 个**。OP 没有指定哪种特定格式,所以我觉得基于对特定文档的假设来否决这个答案是不公平的。 (5认同)
  • [本评论](http://php.net/manual/en/function.fputcsv.php#104980)在[fputcsv()`的文档页面上(http://php.net/manual/en/ function.fputcsv.php)显示了当你想以csv格式输出到浏览器而不是实际文件时如何使用`fputcsv()`. (3认同)
  • @cz 这里的答案显然是错误的。它既没有回答原来的问题,也没有提供合理的其他背景。例如,通常的“其他 CSV 格式”都不需要转义双引号内的单引号,但某些常见的 CSV 阅读器不会转义转义的单引号。对于某些读者来说,转义反斜杠可能是必要的,但同样不是所有读者都需要转义反斜杠。 (3认同)
  • 您不需要实际转义单引号等.正确的CSV文件甚至不需要在仅包含单引号的字段周围添加双引号.如果CSV阅读器已正确实现,即使使用这些符号,也应正确读取文件. (2认同)

qwr*_*qwr 6

理论上,CSV 是一种简单的格式(用逗号分隔的表格数据),但遗憾的是没有正式的规范,因此有许多细微不同的实现。导入/导出时需要小心。我将引用 RFC 4180 来实现常见的实现

2.  Definition of the CSV Format

   While there are various specifications and implementations for the
   CSV format (for ex. [4], [5], [6] and [7]), there is no formal
   specification in existence, which allows for a wide variety of
   interpretations of CSV files.  This section documents the format that
   seems to be followed by most implementations:

   1.  Each record is located on a separate line, delimited by a line
       break (CRLF).  For example:

       aaa,bbb,ccc CRLF
       zzz,yyy,xxx CRLF

   2.  The last record in the file may or may not have an ending line
       break.  For example:

       aaa,bbb,ccc CRLF
       zzz,yyy,xxx

   3.  There maybe an optional header line appearing as the first line
       of the file with the same format as normal record lines.  This
       header will contain names corresponding to the fields in the file
       and should contain the same number of fields as the records in
       the rest of the file (the presence or absence of the header line
       should be indicated via the optional "header" parameter of this
       MIME type).  For example:

       field_name,field_name,field_name CRLF
       aaa,bbb,ccc CRLF
       zzz,yyy,xxx CRLF


   4.  Within the header and each record, there may be one or more
       fields, separated by commas.  Each line should contain the same
       number of fields throughout the file.  Spaces are considered part
       of a field and should not be ignored.  The last field in the
       record must not be followed by a comma.  For example:

       aaa,bbb,ccc

   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"
Run Code Online (Sandbox Code Playgroud)

所以通常

  • 字段可以用双引号括起来,也可以不用双引号括起来。(2005 年的 RFC 说 Excel 不使用双引号,但我用 Excel 2016 进行了测试,结果确实如此。)
  • 包含换行符 (CRLF)、双引号和逗号的字段应括在双引号中。(特别是,CSV 文件可能有多行,因为它们出现在文本编辑器中,对应于一行数据。)
  • 如果使用双引号括住字段,则必须通过在字段前添加另一个双引号来转义出现在字段内的双引号
    • 因此,""在原始 CSV 字段中表示空字符串,""""在原始 CSV 中表示单引号,"

(通常不是问题:CRLF(Windows 风格)或 LF(Unix 风格)换行符;最后一行是否以换行符结束)

但是,您可能会遇到使用转义字符(如 )转义引号或其他字符(分隔符、换行符、转义字符本身)的数据\。例如,在 readr's 中read_csv(),这是由escape_double和控制的escape_backslash。一些不寻常的数据使用注释字符,例如#(R 中默认read.table但不是read.csv)。