Rails FasterCSV“未加引号的字段不允许 或 " [英] Rails FasterCSV "unquoted fields do not allow or "

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

问题描述

我遇到了 FasterCSV 和我的 rake db:seeds 迁移问题.我得到错误:rake 中止!未加引号的字段不允许 或 (第 2 行)"在以下seeds.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 or (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".

我是一个新手,我不知道这是否真的是我需要破解的编码问题(我一直试图这样做没有成功,如果是这样,我真的可以使用一些指导)或者,我更有可能觉得,我犯了一个简单的错误,并且在我设置种子.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 中的所有格式一样简单.Excel 似乎习惯于在保存为 csv 文件后保留大量格式,这导致了失败.在我将所有没有格式的数据复制并粘贴到一个新的 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“未加引号的字段不允许 或 "的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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