CSV - 无引号字段不允许\r或\\\<br/>(第2行) [英] CSV - Unquoted fields do not allow \r or \n (line 2)

查看:274
本文介绍了CSV - 无引号字段不允许\r或\\\<br/>(第2行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解析CSV文件,但仍然收到错误消息无引号字段不允许\r或\\\
(第2行)。


我在这里找到类似的主题,其中有一个提示如下:

  CSV.open 'file.csv',:row_sep =>\r\\\
)do | csv |

但不幸的是他无法使用...我无法更改CSV文件,因此我需要在代码中修复它。



编辑 CSV文件示例:

  A; B; C 
1234; ...

有办法吗?



非常感谢!

解决方案

首先,您应该将列分隔符设置为;,因为这不是正常的CSV文件解析。这对我有用:

  CSV.open('file.csv',:row_sep =>:auto,:col_sep = >;)do | csv | 
csv.each {| a,b,c | puts#{a},#{b},#{c}}
end

从1.9.2 CSV文档:


自动发现预先读取数据,查找下一个 \r\\\

\\\
\r 序列。即使在带引号的字段中出现
,也将选择一个序列,假设您在那里有相同的行尾



Trying to parse a CSV file, but still getting the error message Unquoted fields do not allow \r or \n (line 2)..

I found here at SO similar topic, where was a hint to do following:

  CSV.open('file.csv', :row_sep => "\r\n") do |csv|

but his unfortunately doesn't works me... I can't change the CSV file, so I would need to fix it in the code.

EDIT sample of CSV file:

A;B;C
1234;...

Is there any way to do it?

Many thanks!

解决方案

First of all, you should set you column delimiters to ';', since that is not the normal way CSV files are parsed. This worked for me:

CSV.open('file.csv', :row_sep => :auto, :col_sep => ";") do |csv|
    csv.each { |a,b,c| puts "#{a},#{b},#{c}" } 
end

From the 1.9.2 CSV documentation:

Auto-discovery reads ahead in the data looking for the next \r\n, \n, or \r sequence. A sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there.

这篇关于CSV - 无引号字段不允许\r或\\\<br/>(第2行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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