Ruby: неправильно сформированный CSV при конвертации в json - PullRequest
0 голосов
/ 25 января 2020

Я пытаюсь преобразовать CSV-файл с кодами инцидентов с их описаниями в json файл. со следующим кодом.

require 'csv'
require 'json'

csv = File.open('incidentCodes.json').read
CSV.parse(csv).to_json

File.open("incidentCodes.json", "w") do |f|
  f.write(csv)
end

Каждый раз, когда я запускаю код, хотя он говорит: «CSV :: MalformedCSVError (Недопустимое цитирование в строке 1)»

Это первые несколько строк моего CSV

111, "Building fire. Excludes confined fires."
112, "Fire in structure, other than in a building. Included are fires on or in piers, quays, or pilings: tunnels or under-
ground connecting structures; bridges, trestles, or overhead elevated structures; transformers, power or utility
vaults or equipment; fences; and tents."
113, "Cooking fire involving the contents of a cooking vessel without fire extension beyond the vessel"
114, "Chimney or flue fire originating in and confined to a chimney or flue. Excludes fires that extend beyond the
chimney."

Я пытался обойти использование CSV.parse с другими методами, которые я видел, но тогда он только записывал «[['identCodes.csv ']]» в файл json. Я очень плохо знаком с ruby, поэтому любая помощь - большая помощь.

1 Ответ

1 голос
/ 25 января 2020

У вас есть лишний пробел после запятой.

> CSV.parse '111, "Building fire. Excludes confined fires."'
=> CSV::MalformedCSVError: Illegal quoting in line 1.

> CSV.parse '111,"Building fire. Excludes confined fires."'
=> [["111", "Building fire. Excludes confined fires."]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...