2

Basically I'm reading a large csv file, and it has some mistakes. Few of the columns are separated by dot instead of comma. Does anyone know of a way to still run those lines ? example mistake on line two between "test". "test":

CSV.foreach("#{Rails.root}/tmp/test.csv") do |row|

puts  "Mrs","test","test","U26453"
puts  "Mr","test"."[email protected]","U50406"  - CSV::MalformedCSVError: Missing or stray quote in line 2
end 

Thank you

2
  • 1
    Is this the actual code you are using? Commented Aug 11, 2015 at 11:59
  • this is just a short example of what each row will print out. Commented Aug 11, 2015 at 12:01

1 Answer 1

2

You can write a small regex, which will split by different characters:

split(/[,.]/)

v = '"Mrs","test","test","U26453"'
v.split(/[,.]/)
=> ["\"Mrs\"", "\"test\"", "\"test\"", "\"U26453\""]

UPDATE:

As @gerep noticed, the above will not split correctly when . or , are inside of the substring. As a solution:

split(/"[,.]"/)
Sign up to request clarification or add additional context in comments.

2 Comments

It won't work in case there is a dot or comma in any element, like: "Mr","test.something"."test","U50406"
i need to keep commas and clean text, and the text have some dots in there as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.