From: Phil Meier Date: 2007-08-16T16:15:02+09:00 Subject: Re: removing quotes in a string Grace Xue schrieb: > Thanks for all that everyone. I'm a bit lost after a whole afternoon of > trail and error. What I'm trying to do is read a csv. file that has > different rows and multiple coloumns. The way it's currently done is > putting each row into an array, and retrieve each column using the > element number. > > For example, a file with a line like this (CSV) > 16/08/07,working hard > > My codes are like this: > for row in csv_rows > begin > parsed[] > CSV.parse_row(row, 0, parsed) > date_string = parsed[0] => 16/08/07 > description = parsed[1] => working hard > end > > The problem I have is some file come in as > "16/08/07,working hard" > > I have a feeling that I need to remove the quotes before the > CSV.parse_row line. But how? > > Thanks csv_rows.each do | row | parsed[] if row[0] == ?" row = row[1..-2] end CSV.parse_row(row, 0, parsed) date_string = parsed[0] => 16/08/07 description = parsed[1] => working hard end You have to check row before parsing it. If it has a " at position 0 (at the beginning) just remove the first and the last character of the string. BR Phil