[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: replacing parts of newlines

Xavier Noria

5/2/2007 4:41:00 PM

On May 2, 2007, at 6:26 PM, Mike Steiner wrote:

> I'm writing a script to convert text into a csv file to import into
> MS Excel
> (under Windows), and Excel allows multiline text within a cell, but
> the
> newlines (\x0D\x0A) have to be replaced with only a \x0A for Excel
> to put
> everything into one cell.
>
> The problem is that in Ruby, if I do:
>
> newfield = field.gsub ( "\x0D" , "" )
>
> it doesn't do anything because Ruby treats \x0D\x0A as a unit.

Not really.

If there's a CRLF in the text file and you read it using text mode in
a CRLF platform, the CR is filtered out by the I/O layer. So a line-
oriented script only sees LFs in its strings, vaguely speaking.
Reciprocally, when you write to disk in text model in a CRLF platform
all LFs get a CR automatically prepended. That's transparent to the
programmer, in fact that's done by the very stdio in the C interpreter.

You probably need to work in binmode.

-- fxn