gpt4 book ai didi

ruby - 修复 Ruby 代码中出现 "Illegal quoting"错误的无效 CSV 行

转载 作者:数据小太阳 更新时间:2023-10-29 07:34:38 25 4
gpt4 key购买 nike

出于某种原因,csv 文件中有一些行带有“非法引用”错误,例如:

1336481227,178.108.171.183,3.2.0,9700132ccc02e12a,c083b5d2-ec92-486f-a5b3-512dba1ce4ae,invoke_action,"{""timestamp"":""2012-05-08 13:47:26""} "

1336481227,178.108.171.183,3.2.0,9700132ccc02e12a,c083b5d2-ec92-486f-a5b3-512dba1ce4ae,invoke_action,{""timestamp"":""2012-05-08 13:47:27""

第一行是正确的。但是第二行的最后一个字段 {""timestamp"":""2012-05-08 13:47:27""} 缺少大括号外的双引号,所以当我尝试

CSV.foreach(csv_file_path) do |row|
puts "======================="
puts row
puts "======================="
end

我遇到了错误

=======================
1336481227
178.108.171.183
3.2.0
9700132ccc02e12a
c083b5d2-ec92-486f-a5b3-512dba1ce4ae
invoke_action
{"timestamp":"2012-05-08 13:47:26","a":"b"}
=======================
#<CSV::MalformedCSVError: Illegal quoting in line 2.>

无论如何,我可以用这样的问题修复这一行,还是在发生错误时跳过它?

编辑:如果我尝试

CSV.foreach(csv_file_path, :quote_char => "\'") do |row|
puts "======================="
puts row
puts "======================="
end

虽然第一行的 JSON 格式值被破坏了:

=======================
1336481227
178.108.171.183
3.2.0
9700132ccc02e12a
c083b5d2-ec92-486f-a5b3-512dba1ce4ae
invoke_action
"{""timestamp"":""2012-05-08 13:47:26""
""a"":""b""}"
=======================
=======================
1336481227
178.108.171.183
3.2.0
9700132ccc02e12a
c083b5d2-ec92-486f-a5b3-512dba1ce4ae
invoke_action
{""timestamp"":""2012-05-08 13:47:27""}
=======================

最佳答案

尝试

CSV.foreach(csv_file_path, :quote_char => "\'")

关于ruby - 修复 Ruby 代码中出现 "Illegal quoting"错误的无效 CSV 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com