Ruby CSV-第1行中的引用非法.CSV :: MalformedCSVError [英] Ruby CSV - Illegal quoting in line 1. CSV::MalformedCSVError

查看:249
本文介绍了Ruby CSV-第1行中的引用非法.CSV :: MalformedCSVError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从csv文件读取时遇到问题.文件来自Windows,因此我认为存在一些编码问题.我的代码如下:

I have a problem with reading from the csv file. File comes from Windows, so I suppose there are some encoding issues. My code looks like this:

CSV.open(path, 'w', headers: :first_row, col_sep: ';', row_sep: "\r\n", encoding: 'utf-8') do |csv|    
    CSV.parse(open(doc.file.url), headers: :first_row, col_sep: ';', quote_char: "\"", row_sep: "\r\n", encoding: 'utf-8').each_with_index do |line, index| 

        csv << line.headers if index == 0

        # do something wiht row

        csv << line 
    end
end

我必须打开现有文件并从中完成一些列.所以我只创建一个新文件.现有文件存储在Dropbox上,因此我必须使用open方法.

I have to open existing file and complete some columns from it. So I just create new file. The existing file is stored on Dropbox, so I have to use open method.

问题是我在这一行中得到一个错误:

The problem is that I get an error in this line:

 CSV.parse(open(doc.file.url), headers: :first_row, col_sep: ';', quote_char: "\"", row_sep: "\r\n", encoding: 'utf-8').each_with_index do |line, index| 

错误是:

  Illegal quoting in line 1. CSV::MalformedCSVError

我检查了一下,似乎文件中没有BOM字符(不确定是否检查正确).问题似乎出在引号字符中.文件中的每一行都会引发异常.

I check and seems like I don't have BOM characters in the file (not sure if check it right). The problem seems to be in quote character. The exception is thrown for every line in the file.

这是导致我出现问题的文件: https://dl.dropboxusercontent. com/u/3900955/geo_bez_adresu_10_do_testow_small.csv

This is the file that causes me problems: https://dl.dropboxusercontent.com/u/3900955/geo_bez_adresu_10_do_testow_small.csv

我尝试了不同于StackOverflow的方法,但没有任何帮助,例如,我将代码更改为:

I tried different approaches from StackOverflow but nothing helps, for example I changed my code into this:

CSV.open(path, 'w', headers: :first_row, col_sep: ';', row_sep: "\r\n", encoding: 'utf-8') do |csv|
    open(doc.file.url) do |f|
        f.each_line do |line|
            CSV.parse(line, 'r:bom|utf-8') do |row|
               csv << row
            end
        end
    end
end 

但是没有帮助.对于解析此文件的任何帮助,我将不胜感激.

but it doesn't help. I will be grateful for any help with parsing this file.

========编辑========

======= edit =========

当我在Windows上使用UTF-8(在Notepad ++中)对ANSI编码的同一个文件进行安全保护时,可以正确解析该文件.在此讨论中,看来我在原始文件中有BOM.如何在Ruby中检入文件是否包含BOM表以及如何使用BOM表解析csv文件?

When I safe the same file on Windows with encoding ANSI as UTF-8 (in Notepad++) I can parse the file correctly. From this discussion What is "ANSI as UTF-8" and how can I make fputcsv() generate UTF-8 w/BOM?, it seems like I have BOM in the original file. How I can check in Ruby if my file is with BOM and how I can parse the csv file with BOM ?

推荐答案

如果找不到包含BOM的远程文件,我找不到任何直接读取方法.因此,我使用Tempfile文件创建临时文件,然后使用'r:bom | utf-8'进行CSV.open:

I didn't find any way to read directly from remote file, if it contains BOM. So I use Tempfile file to create temporary file and then I do CSV.open with 'r:bom|utf-8':

doc = Document.find(doc_id)

path = "#{Rails.root.join('tmp')}/#{doc.name.split('.').first}_#{Time.now.to_i}.csv"

file = Tempfile.new(["#{doc.name.split('.').first}_#{Time.now.to_i}", '.csv']) 
file.binmode
file << open(doc.file.url).read
file.close

CSV.open(path, 'w', headers: :first_row, col_sep: ';', row_sep: "\r\n", encoding: 'utf-8') do |csv|
    CSV.open(file.path, 'r:bom|utf-8', headers: :first_row, col_sep: ';', quote_char: "\"", row_sep: "\r\n").each_with_index do |line, index| 

    # do something

    end
end 

现在,它似乎可以解析文件了.

Now, it seems to parse the file.

这篇关于Ruby CSV-第1行中的引用非法.CSV :: MalformedCSVError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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