简体   繁体   English

引用字段在smarter_csv中转义了

[英]Quoted fields escaped in smarter_csv

I have a CSV file that I'm trying to process with smarter_csv in Ruby. 我有一个CSV文件,正尝试在Ruby中使用smarter_csv处理。 Every field has double quuotes and there are some fields that have double quotes nested within them that are not escaped. 每个字段都有双引号,并且有些字段中嵌套了双引号,并且没有转义。 I'm using :force_simple_split => true as suggested in the documentation to fix this situation. 我正在按照文档中的建议使用:force_simple_split => true来解决此问题。 However, when I try to process the file every field has escaped quotes within it. 但是,当我尝试处理文件时,每个字段都在其中转义了引号。 What am I doing wrong here? 我在这里做错了什么?

I'm opening a csv file that was generated from a Windows server that looks something like this... 我正在打开从Windows服务器生成的csv文件,看起来像这样...

header1,header2,header3 "field1, There are "nested quotes" here.","field2", "field3"

I open the file with smarter_csv like so... 我像这样用smarter_csv打开文件...

c = SmarterCSV.process('myfile.csv', force_simple_split: true, file_encoding: "iso-8859-1", row_sep: "\\r")

Then I get output like this... 然后我得到这样的输出...

{:header1=>"\\"field1, There are \\"nested quotes\\"", :header2=>"\\"field2\\"", :header3=>"\\"field3\\"" }

> SmarterCSV::VERSION
 => "1.0.19"
> options = {:force_simple_split => true}
> ap SmarterCSV.process('/tmp/quoted.csv', options)
[
   {
        :header1 => "\"field1",
        :header2 => "There are \"nested quotes\" here.\"",
        :header3 => "\"field2\""
    }
]

force_simple_split does just that .. it does not allow for embedded :col_sep characters in your data. force_simple_split只是这样做..它不允许在数据中嵌入:col_sep字符。

Please note that your example CSV data contains an embedded comma, and that the simple split of the line with the data results in four fields, not three. 请注意,您的示例CSV数据包含一个嵌入式逗号,并且用数据对行进行简单的拆分会导致四个字段,而不是三个字段。

Because you don't provide a fourth header, the fourth field is ignored. 因为您没有提供第四个标题,所以将忽略第四个字段。

Please open an issue for correct handling of quoted data as in your example 请打开一个问题,以正确处理引用的数据,如您的示例

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

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