- 1 year ago
Is there a way to make Ruby CSV gem generate CSVs with Windows (CR LF) End Of Lines?
Comment
1 / 1
Yes, you can configure the Ruby CSV gem to generate CSV files with Windows-style (CR LF) end-of-line characters by setting the row_sep
option to "\r\n"
.
Here's how you can do it:
rubyrequire 'csv'
csv_data = [
['Name', 'Age', 'City'],
['John', '25', 'New York'],
['Jane', '30', 'Los Angeles']
]
CSV.open('output.csv', 'wb', row_sep: "\r\n") do |csv|
csv_data.each do |row|
csv << row
end
end
In this example, the row_sep: "\r\n"
option is passed to the CSV.open
method. This will ensure that the generated CSV file uses Windows-style line endings.
Keep in mind that this setting applies to writing CSV files. When reading CSV files with the Ruby CSV gem, it will automatically handle different line ending formats.