如何使用 FasterCSV 导入名称类似于“Ciarán”的行?

发布于 2024-08-02 15:22:18 字数 523 浏览 4 评论 0原文

我正在尝试在数据迁移中加载成员数据库。相当多的名字都有特殊字符,例如“Ciarán”。我设置了一个像这样的简单示例:

require 'rubygems'
require 'fastercsv'

FasterCSV.foreach("/Users/developer/Work/madmin/db/data/Members.csv") do |row|
  puts row.inspect
end

我得到以下信息:

/usr/local/lib/ruby/gems/1.8/gems/fastercsv-1.5.0/lib/faster_csv.rb:1616:in `shift': FasterCSV::MalformedCSVError (FasterCSV::MalformedCSVError)

当我点击具有该名称的行时,

我一直在谷歌搜索字符编码和UTF-8,但尚未找到解决方案。我想保留特殊字符,但不想编辑每个失败的成员名称。

非常感谢, 布雷特

I am trying to load in my data migration a member database. Quite a few of the names have special characters such as "Ciarán". I've set up a simple example like this:

require 'rubygems'
require 'fastercsv'

FasterCSV.foreach("/Users/developer/Work/madmin/db/data/Members.csv") do |row|
  puts row.inspect
end

and I get the following:

/usr/local/lib/ruby/gems/1.8/gems/fastercsv-1.5.0/lib/faster_csv.rb:1616:in `shift': FasterCSV::MalformedCSVError (FasterCSV::MalformedCSVError)

when I hit the row with this name.

I have been googling character encoding and UTF-8, but have not yet found a solution. I'd like to keep the special characters but would rather not have to edit each member name that fails.

Many thanks,
Brett

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-08-09 15:22:18

它对我来说立即有效,但如果您需要更改编码,您可以将编码选项传递给 FasterCSV。例如,要告诉它使用 UTF-8,您可以执行以下操作:

require 'rubygems'
require 'fastercsv'

FasterCSV.foreach("some file.csv", :encoding => 'u') do |row|
  puts row.inspect
end

It works right off the bat for me, but if you need to change the encoding, you can pass an encoding option to FasterCSV. For example, to tell it to use UTF-8, you can do this:

require 'rubygems'
require 'fastercsv'

FasterCSV.foreach("some file.csv", :encoding => 'u') do |row|
  puts row.inspect
end

The encoding options are listed in the documentation for new.

不寐倦长更 2024-08-09 15:22:18

我在其他地方读到可以通过设置 KCODE 来解决这个问题。例如:

$KCODE = "U"

将其粘贴在顶部。

James Edward Gray 还表示,他为 FasterCSV 添加了编码支持,但仅在主干中。

I've read elsewhere that this can be fixed by setting KCODE. For example:

$KCODE = "U"

Stick this at the top.

James Edward Gray has also said he's added encoding support to FasterCSV but it's in trunk only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文