Line/Row separator/delimiter in exported CSV using Ruby CSV

1.7k Views Asked by At

Is it possible to change the default line separator from newline '\n' or '\r\n' to some other character, e.g '|' while importing.

I know this seems foolish. But i have multiple records in csv file with column address and some addresses might contain '\n' in information.

eg Corner Case X|X|X|X|123 New Addr|X|X|X|

Normal Case X|X|X|X|123|X|X|X|

1

There are 1 best solutions below

0
Max On

Yes, you can change the default separators.

require 'csv'
CSV.read(csv_file, row_sep: ?|)

But that won't help you because regardless of the separators, the CSV library does not allow \r or \n in unquoted fields.

There's always the poor man's parser:

File.read(file).split(row_sep).map { |row| row.split(col_sep) }