# Problem for CSV import

**URL:** <https://www.ruby-forum.com/t/problem-for-csv-import/186797>\
**Category:** Rails\
**Created:** [April 12, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/problem-for-csv-import/186797 "2010-04-12T15:23:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 12, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/problem-for-csv-import/186797/1 "2010-04-12T15:23:12Z")

</div>

Hi,  
I am doing a finctionality to import csv file.  
My CSV file is like this  
Header-- Name,Address,Details  
Data-- Tushar, sangavi,pune, I am a ror developer

My controller code is:-  
@parsed\_file=CSV::Reader.parse(params[:dump][:file])  
@parsed\_file.each do |row|  
p row[1]  
p row[2]  
end

My problem is if you looked at it that address is “sangavi,pune”. But  
whenever I am printing the row values. I am getting row[1] as “sangavi”  
and row[2] as “pune”, which is not correct, Can anyoneone tell me how  
should I solve this problem?

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 12, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/problem-for-csv-import/186797/2 "2010-04-12T15:45:54Z")

</div>

> My problem is if you looked at it that address is “sangavi,pune”. But  
> whenever I am printing the row values. I am getting row[1] as “sangavi”  
> and row[2] as “pune”, which is not correct, Can anyoneone tell me how  
> should I solve this problem?

Get the original data in a better format than CSV?

Get the provider of the original data to export to CSV properly so  
address  
becomes “sangavi,pune” rather than sangavi,pune?

Make a guess (probably incorrect) that the address may have multiple  
commas,  
but the details won’t - then do pre-processing on the file (or stream)  
using  
regexes to wrap the address in quotes?

GOOD LUCK!

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 12, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/problem-for-csv-import/186797/3 "2010-04-12T18:50:09Z")

</div>

Tushar G. wrote:

> Hi,  
> I am doing a finctionality to import csv file.  
> My CSV file is like this  
> Header-- Name,Address,Details  
> Data-- Tushar, sangavi,pune, I am a ror developer
> 
> My controller code is:-  
> @parsed\_file=CSV::Reader.parse(params[:dump][:file])  
> @parsed\_file.each do |row|  
> p row[1]  
> p row[2]  
> end
> 
> My problem is if you looked at it that address is “sangavi,pune”. But  
> whenever I am printing the row values. I am getting row[1] as “sangavi”  
> and row[2] as “pune”, which is not correct, Can anyoneone tell me how  
> should I solve this problem?

The data is being parsed correctly, it’s the data format that is  
incorrect.

For sanity’s sake, strings should always be delimited by double-quotes  
in CSV, as in:

“Tushar”,“sangavi, pune”,“I am a ror developer”

The line

Tushar, sangavi, pune, I am a ror developer

has 4 elements which are comma separated. Given that one of your fields  
is ‘details’, what happens if the data is:

Tushar, sangavi, pune, I am a ror developer, and I like curry, cricket,  
and Bollywood movies.

Enclose those strings in quotes.
