【发布时间】:2019-01-29 00:12:00
【问题描述】:
我正在为我的 Rails 应用程序使用 Ruby 中的内置 CSV 函数。我正在调用一个 URL(通过 HTTParty)来解析它,并尝试将结果保存到我的数据库中。
问题是,我收到错误Unquoted fields do not allow \r or \n,这表明通常输入数据有问题,但在检查数据时,我找不到任何问题。
我是这样检索数据的:
response = HTTParty.get("http://" + "weather.com/ads.txt", limit: 100, follow_redirects: true, timeout: 10)
(此数据可在网址 weather.com/ads.txt 上公开获得)
然后我尝试解析数据,应用一些正则表达式来忽略 # 之后的所有内容,忽略空行等。
if response.code == 200 && !response.body.match(/<.*html>/)
active_policies = []
CSV.parse(response.body, skip_blanks: true, skip_lines: /(^\s*#|^\s*$|^contact=|^CONTACT=|^subdomain=)/) do |row|
begin
#print out the individual ads.txt records
puts ""
print row[0].downcase.strip + " " + row[1].strip + " " +
row[2].split("#").first.strip
active_policies.push(
publisher.policies.find_or_create_by(ad_partner: row[0].downcase.strip, external_seller_id: row[1].strip, seller_relationship: row[2].split("#").first.strip) do |policy|
policy.deactivated_at = nil
end
)
rescue => save
#Add error event to the new sync status model
puts "we are in the loop"
puts save.message, row.inspect, save.backtrace
next
end
end
#else
#puts "Too many policies. Skipping " + publisher.name
#end
#now we are going to run a check to see if we have any policies that are outdated, and if so, flag them as such.
deactivated_policies = publisher.policies.where.not(id: active_policies.map(&:id)).where(deactivated_at: nil)
deactivated_policies.update_all(deactivated_at: Time.now)
deactivated_policies.each do |deactivated_policy|
puts "Deactivating Policy for " + deactivated_policy.publisher.name
end
elsif response.code == 404
print
print response.code.to_s + " GET, " + response.body.size.to_s + " body, "
puts response.headers.size.to_s + " headers for " + publisher.name
elsif response.code == 302
print response.code.to_s + " GET, " + publisher.name
else
puts response.code.to_s + " GET ads txt not found on " + publisher.name
end
publisher.update(last_scan: Time.now)
rescue => ex
puts ex.message, ex.backtrace, "error pulling #{publisher.name} ..."
#publisher.update_columns(active: "false")
end
end`
我的一些想法/调查结果:
-
我已尝试逐行浏览这一行,我发现第 134 行是中断扫描的原因。我通过像这样进行手动检查来做到这一点:
CSV.parse(response.body.lines[140..400].join("\n"), skip_blanks: true, skip_lines: /(^\s*#|^\s*$|^contact=|^CONTACT=|^subdomain=)/)但这对我没有帮助,因为即使我将第 134 行标识为违规行,我也不知道如何检测或处理它。我注意到源文件(位于 weather.com/ads.txt)有不寻常的字符,但即使通过
response.body.force_encoding("UTF-8")将其强制为 utf-8 仍然会引发错误。我尝试将
next添加到救援块,因此即使它发现错误,它也会移动到 csv 中的下一行,但这不会发生 - 它只是出错并停止解析 - 所以我得到了前 130~ 个条目,但没有得到剩余的条目。类似于页面类型,我不确定页面类型是 HTML 而不是文本文件是否会在此处产生问题。
我很想知道如何检测和处理此错误,因此欢迎提出任何想法!
作为参考,#PBS 显然是在源文件中给我带来麻烦的第 134 行,但我不知道我是否完全相信这是问题所在。
#canada
google.com, pub-0942427266003794, DIRECT, f08c47fec0942fa0
indexexchange.com, 184315, DIRECT
indexexchange.com, 184601, DIRECT
indexexchange.com, 182960, DIRECT
openx.com, 539462051, DIRECT, 6a698e2ec38604c6
#spain
#PBS
google.com, pub-8750086020675820, DIRECT, f08c47fec0942fa0
google.com, pub-1072712229542583, DIRECT, f08c47fec0942fa0
appnexus.com, 3872, DIRECT
rubiconproject.com, 9778, DIRECT, 0bfd66d529a55807
openx.com, 539967419, DIRECT, 6a698e2ec38604c6
openx.com, 539726051, DIRECT, 6a698e2ec38604c6
google.com, pub-7442858011436823, DIRECT, f08c47fec0942fa0
【问题讨论】:
标签: ruby-on-rails ruby csv parsing error-handling