Hi,
I have code that successfully reads a CSV file using FasterCSV and puts
the data into a simple, 2x2 array.
Some of the cells I read in and print out to a file contain some kind of
a control character that I don't know how to strip out and substitute
with formal line feeds or carriage returns.
Data in original spreadsheet cell that gets read in as CSV reads:
"...
1. String A
2. String B
..."
Data, after reading CSV file and printing out cell to a file reads:
"...
1. String A^M^MString B
..."
My problem is that I need to get the latter output to look like the
original data.
Does anyone know a quick and simple way to parse the string for the
control character "^M^M" and replace it with a formal line feed or
carriage return?
Thanks for your help,
Frank
···
--
Posted via http://www.ruby-forum.com/.
Hi!
Frank Guerino schrieb:
I have code that successfully reads a CSV file using FasterCSV and puts
the data into a simple, 2x2 array.
[...}
Data in original spreadsheet cell that gets read in as CSV reads:
"...
1. String A
2. String B
.."
Data, after reading CSV file and printing out cell to a file reads:
"...
1. String A^M^MString B
.."
My problem is that I need to get the latter output to look like the
original data.
[...]
The "^M" should represent a "carridge return" control character.
The CR of a CRLF linebreak, see [1]. In a ruby regular
expression, ^M should be matched by \r.
[1] Newline - Wikipedia
It seems like different or wrong types of newlines/linebreaks
inside the CSV file (mixed CRLF and LF, ...).
Since I don't know FasterCSV, there may be a solution for this
within FasterCSV.
If not, replacing ^M^M with a single newline might help:
data.gsub(/\r\r/, "\n")
Regards,
Daniel
···
--
Nicht das, was wir nicht wissen, bringt uns zu Fall ...
sondern das, was wir fälschlicherweise zu wissen glauben.
(Tom DeMarco, Der Termin)