Last active
April 11, 2023 19:52
-
-
Save westonganger/d340600f88e41574f538de8a67f31ba7 to your computer and use it in GitHub Desktop.
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
### Article about the BOM: https://estl.tech/of-ruby-and-hidden-csv-characters-ef482c679b35 | |
### Article about other CSV gotchas: https://blog.rubyhero.dev/solving-problems-with-csv-parsing | |
### Remove Blank Rows from end of file | |
### Ideally this would be handled by the controller to sanitize before saving upload however I wasnt able to make that work | |
file_data = file.read.sub(/[,\n\r\t ]*\z/, "") | |
i = -1 | |
csv_bom = "\xEF\xBB\xBF".force_encoding('UTF-8') ### https://estl.tech/of-ruby-and-hidden-csv-characters-ef482c679b35 | |
CSV.parse(file_data, headers: false) do |row_data| | |
i += 1 | |
sanitized_row = row_data.map{|x| x&.strip } | |
if i == 0 && sanitized_row[0].to_s.force_encoding('UTF-8').start_with?(csv_bom) | |
sanitized_row[0] = sanitized_row[0].sub(csv_bom, '') | |
end | |
@csv_row_data << sanitized_row | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment