Rails FasterCSV中的“未引用字段"不允许\ r或\ n [英] Rails FasterCSV "unquoted fields do not allow \r or \n"

查看:98
本文介绍了Rails FasterCSV中的“未引用字段"不允许\ r或\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FasterCSV和rake db:seeds迁移时遇到问题.我得到了错误: "rake中止!未引用的字段不允许\ r或\ n(第2行)" 根据以下seed.rb数据:

I'm having an issue with FasterCSV and my rake db:seeds migration. I get the error: "rake aborted! Unquoted fields do not allow \r or \n (line 2)" on the following seeds.rb data:

require 'csv' 

directory = "db/init_data/"

file_name = "gardenzing020812.csv"
path_to_file = directory + file_name
puts 'Loading Plant records'
# Pre-load all Plant records
n=0
CSV.foreach(path_to_file) do |row|
  Plant.create! :name => row[1],
  :plant_type => row[3],
  :group => row[2],
  :image_path => row[45],
  :height => row[5],
  :sow_inside_outside => row[8]
n=n+1
end                 

我一直在寻找解决此问题的方法,发现对于很多人来说,这是UTF-8编码问题.我试过要求iconv和:encoding =>'u',但是那给了我错误"UTF-8中无效的字节序列".

I've searched for a solution to this problem and have discovered that for a lot of folks it's a UTF-8 encoding problem. I've tried requiring iconv and :encoding => 'u', but that then gives me the error "invalid byte sequence in UTF-8".

我是新手,我无法弄清楚这是否真的是我需要破解的编码问题(我一直尝试解决不成功,如果可以的话,我真的可以使用一些指导),或者,我更可能觉得自己犯了一个简单的错误,并且在设置seeds.rb以及可能的excel-> csv文件的方式上做了一些错误. csv文件中没有错误或尴尬的数据.它是简单的一词字符串,文本和整数.请帮忙!

I'm a newbie, and I can't figure out if it's really an encoding issue that I need to crack (which I've been trying to do unsuccessfully and if so, I could really use some guidance) or, more likely I feel, that I've made a simple misstep and done something wrong with the way I've set up seeds.rb and possibly my excel -> csv file. There's no bad or awkward data in the csv file. It's simple one-word strings, text and integers. Please help!

推荐答案

就像清除csv中的所有格式一样简单.保存到csv文件中后,Excel似乎习惯保留很多格式,这是导致失败的原因.将所有没有格式的数据复制并粘贴到新的csv文件中之后,就可以了.

It was as simple as clearing all the formatting off in the csv. Excel seems to have a habit of retaining a lot of the formatting after saving in a csv file, which was causing the failure. After I copied and pasted all the data with no formatting in a new csv file, it was fine.

这篇关于Rails FasterCSV中的“未引用字段"不允许\ r或\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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