^M^M Control Character Substitution

F

Frank Guerino

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
 
D

Daniel Schömer

Hi!

Frank said:
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] http://en.wikipedia.org/wiki/Newline

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top