Created
July 10, 2018 20:07
-
-
Save Geesu/99c743c2c489c181982673d9e222777c to your computer and use it in GitHub Desktop.
Detect invalid characters in a CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contents = File.read('/Users/Josh/Desktop/1.csv') | |
unless contents.valid_encoding? | |
invalid_characters = {} | |
contents.each_line.with_index do |line, idx| | |
invalid_characters[idx] ||= [] | |
line.each_char do |char| | |
unless char.valid_encoding? | |
invalid_characters[idx] << char | |
end | |
end | |
end | |
invalid_characters.select!{ |_line, characters| characters.any? } | |
invalid_characters.each do |line, characters| | |
if characters.any? | |
puts "Line #{line}: #{characters.join(',')}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment