【问题标题】:File.readlines invalid byte sequence in UTF-8 (ArgumentError)File.readlines UTF-8 中的无效字节序列(ArgumentError)
【发布时间】:2013-08-20 21:20:06
【问题描述】:

我正在处理一个包含来自网络的数据的文件,并在某些日志文件上遇到 invalid byte sequence in UTF-8 (ArgumentError) 错误。

a = File.readlines('log.csv').grep(/watch\?v=/).map do |s|
s = s.parse_csv;
{ timestamp: s[0], url: s[1], ip: s[3] }
end
puts a

我正试图让这个solution 工作。我见过人们这样做

.encode!('UTF-8', 'UTF-8', :invalid => :replace)

但它似乎不适用于File.readlines。

File.readlines('log.csv').encode!('UTF-8', 'UTF-8', :invalid => :replace).grep(/watch\?v=/)

' : 未定义的方法 `encode!'对于 # (NoMethodError)

在文件读取期间过滤/转换无效 UTF-8 字符的最直接方法是什么?

尝试 1

试过了,但失败了,同样的无效字节序列错误。

IO.foreach('test.csv', 'r:bom|UTF-8').grep(/watch\?v=/).map do |s|
  # extract three columns: time stamp, url, ip
  s = s.parse_csv;
  { timestamp: s[0], url: s[1], ip: s[3] }
end

解决方案

这似乎对我有用。

a = File.readlines('log.csv', :encoding => 'ISO-8859-1').grep(/watch\?v=/).map do |s|
s = s.parse_csv;
{ timestamp: s[0], url: s[1], ip: s[3] }
end
puts a

Does Ruby provide a way to do File.read() with specified encoding?

【问题讨论】:

  • 可能是BOM问题,可以'r:bom|utf-8'模式打开文件解决。请尝试一下。
  • @padde 用我的尝试更新了代码。谢谢
  • 您确实应该使用 CSV 而不是 File。这就是它的用途。
  • @pguardiario 是的,我最初看过那个。您能否为上述替代方案提供一个示例。谢谢。
  • 下次以答案的形式发布解决方案。

标签: ruby


【解决方案1】:

我正在努力让这个解决方案发挥作用。我见过人们这样做

   .encode!('UTF-8', 'UTF-8', :invalid => :replace)

但它似乎不适用于 File.readlines。

File.readlines 返回一个数组。数组没有编码方法。另一方面,字符串确实有一种编码方法。

您能否为上述替代方案提供一个示例。

require 'csv'

CSV.foreach("log.csv", encoding: "utf-8") do |row|
  md = row[0].match /watch\?v=/
  puts row[0], row[1], row[3] if md
end

或者,

CSV.foreach("log.csv", 'rb:utf-8') do |row|

如果您需要更快的速度,请使用 fastercsv gem。

这似乎对我有用。

File.readlines('log.csv', :encoding => 'ISO-8859-1')

是的,要读取文件,您必须知道其编码。

【讨论】:

    【解决方案2】:

    在我的情况下,脚本默认为 US-ASCII,我不能随意在服务器上更改它,以免发生其他冲突。

    我做到了

    File.readlines(email, :encoding => 'UTF-8').each do |line|
    

    但这不适用于一些日文字符,所以我在下一行添加了这个,效果很好。

    line = line.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
    

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 2016-07-05
      • 1970-01-01
      • 2012-06-19
      • 2017-03-31
      • 2012-12-11
      • 2023-04-03
      • 2022-10-05
      • 2014-01-09
      相关资源
      最近更新 更多