From: James Edward Gray II Date: 2010-11-05T04:45:17+09:00 Subject: Re: 1.9 CSV Parsing Issues On Nov 4, 2010, at 2:26 PM, Kenny Lam wrote: > File.read shows "field1,field2\nfield3,field4\n" Great. That's what we expected to see. You are right about the content. > I have played around with the some of the other methods and have > determined that this problem only seems to occur when using CSV::open > and then looped through with CSV::each. CSV::foreach and CSV::parse > seem fine. Ah, and let me guess, you always pass a read mode of 'r' to open(), right? CSV is clever and it shuts off Ruby's line ending translation on Windows using 'rb' if you don't specify a mode. By specify a mode, you leave this feature on which allows Ruby to switch \r\n to \n as it did with the read above. > Unfortunately, I need to use CSV::open because I need a > reference to the opened file object in order to do some file cursor > manipulation. No worries, open() is going to work for you. > Other things I have noted is that when running CSV.open('file','r') the > result is show: > <#CSV io_type:File io_path:"/log/test.log" encoding:CP850 lineno:0 > col_sep:"," row_sep:"\r\n" quote_char:"\""> > > While CSV.open('test.log','r',:row_sep => '\r\n') shows result: > <#CSV io_type:File io_path:"/log/test.log" encoding:CP850 lineno:0 > col_sep:"," row_sep:"\\r\\n" quote_char:"\""> > > The double backslashes make me question if the escape character is being > processed correctly. I am relatively new to Ruby, am I using the > language incorrectly or is this a bug? You have a misunderstanding of Ruby Strings. Double quotes allow for escapes like \r or \n, but single quotes do not. You've set the :row_sep to literally slash, r, slash, and n. I image all you need to do is switch your open() call to: CSV.open('path/to/file') The library should take it from there. Hope that helps. James Edward Gray II