Hi, First of all very sorry for this long post. I am trying to save my csv data to table items which is associated with Item model. This is what my csv have: ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; ''description'';''cost_price'';''selling_price'' ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description here!!'';''780'';''890'' ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description here!!'';''8660'';''9790'' First row show the fields for items table. Here I am using fastercsv to process my csv and paperclip to upload. I am able to process file read content and able to fill up the field too here is the processing code: def proc_csv @import = Import.find(params[:id]) @lines = parse_csv_file(@import.csv.path) @lines.shift @lines.each do |line , j| unless line.nil? line_split = line.split(";") unless ((line_split[0].nil?) or (line_split[1].nil?) or (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or (line_split[5].nil?)) # I used puts to get to know about what''s going on. puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 @item = [:name => line_split[0], :number => line_split[1], :sub_category_id => line_split[2],:category_id => line_split[3],:quantity => line_split[4], :sku => line_split[5], :description => line_split[6], :cost_price => line_split[7], :selling_price => line_split[8]] puts "#"*100+"@item is: #{@item.inspect}"+"#"*100 end end end redirect_to import_path(@import) end but the problem is that when it process it and when I check the @item in console it looks like this: ####################################################################################################@item is: [{:quantity=>"\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", :cost_price=>"\000''\0007\0008\0000\000''\000", :number=>"\000''\0005\0004\000''\000", :selling_price=>"\000''\0008\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### ####################################################################################################@item is: [{:quantity=>"\000''\0009\0008\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0002\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", :cost_price=>"\000''\0008\0006\0006\0000\000''\000", :number=>"\000''\0007\0004\000''\000", :selling_price=>"\000''\0009\0007\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### Can anyone kindly tell me why am I getting this kind of string instead of simple string I entered in my csv file? And because of this it''s not being saved into the table too, I have tried all possible formats but nothing seems to be working. I want "Uploaded Item Number 1" instead of "\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000" . Any help will be appreciated. Thanks in advance :) -- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, First of all very sorry for this long post. I am trying to save my csv data to table items which is associated with Item model. This is what my csv have: ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; ''description'';''cost_price'';''selling_price'' ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description here!!'';''780'';''890'' ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description here!!'';''8660'';''9790'' First row show the fields for items table. Here I am using fastercsv to process my csv and paperclip to upload. I am able to process file read content and able to fill up the field too here is the processing code: def proc_csv @import = Import.find(params[:id]) @lines = parse_csv_file(@import.csv.path) @lines.shift @lines.each do |line , j| unless line.nil? line_split = line.split(";") unless ((line_split[0].nil?) or (line_split[1].nil?) or (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or (line_split[5].nil?)) # I used puts to get to know about what''s going on. puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 @item = [:name => line_split[0], :number => line_split[1], :sub_category_id => line_split[2],:category_id => line_split[3],:quantity => line_split[4], :sku => line_split[5], :description => line_split[6], :cost_price => line_split[7], :selling_price => line_split[8]] puts "#"*100+"@item is: #{@item.inspect}"+"#"*100 end end end redirect_to import_path(@import) end but the problem is that when it process it and when I check the @item in console it looks like this: ####################################################################################################@item is: [{:quantity=>"\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", :cost_price=>"\000''\0007\0008\0000\000''\000", :number=>"\000''\0005\0004\000''\000", :selling_price=>"\000''\0008\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### ####################################################################################################@item is: [{:quantity=>"\000''\0009\0008\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0002\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", :cost_price=>"\000''\0008\0006\0006\0000\000''\000", :number=>"\000''\0007\0004\000''\000", :selling_price=>"\000''\0009\0007\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### Can anyone kindly tell me why am I getting this kind of string instead of simple string I entered in my csv file? And because of this it''s not being saved into the table too, I have tried all possible formats but nothing seems to be working. I want "Uploaded Item Number 1" instead of "\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000" . Any help will be appreciated. Thanks in advance :) -- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Jason King
2011-Mar-23 16:04 UTC
Re: Problem in saving fields to database from csv using fastercsv
This is not the right place for this sort of question, use the rubyonrails-talk group instead. On Mar 23, 2011 8:45 AM, "Surya" <raj.surya19@gmail.com> wrote:> Hi, > > First of all very sorry for this long post. I am trying to save my csvdata> to table items which is associated with Item model. > > This is what my csv have: > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > ''description'';''cost_price'';''selling_price'' > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''SomeDescription> here!!'';''780'';''890'' > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description > here!!'';''8660'';''9790'' > > > First row show the fields for items table. > > Here I am using fastercsv to process my csv and paperclip to upload. > > I am able to process file read content and able to fill up the field too > here is the processing code: > > def proc_csv > @import = Import.find(params[:id]) > @lines = parse_csv_file(@import.csv.path) > @lines.shift > @lines.each do |line , j| > unless line.nil? > line_split = line.split(";") > unless ((line_split[0].nil?) or (line_split[1].nil?) or > (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or > (line_split[5].nil?)) > # I used puts to get to know about what''s going on. > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > @item = [:name => line_split[0], :number => line_split[1], > :sub_category_id => line_split[2],:category_id => line_split[3],:quantity=>> line_split[4], :sku => line_split[5], :description => line_split[6], > :cost_price => line_split[7], :selling_price => line_split[8]] > puts "#"*100+"@item is: #{@item.inspect}"+"#"*100 > > end > end > end > redirect_to import_path(@import) > end > > > but the problem is that when it process it and when I check the @item in > console it looks like this: > >####################################################################################################@item> is: [{:quantity=>"\000''\0006\0007\000''\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000\0001\000''\000",> :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > :cost_price=>"\000''\0007\0008\0000\000''\000", > :number=>"\000''\0005\0004\000''\000", > :selling_price=>"\000''\0008\0009\0000\000''\000", > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > :description=>"\000''\000S\000o\000m\000e\000 > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > \000h\000e\000r\000e\000!\000!\000''\000", >:category_id=>"\000''\000W\000N\000''\000"}]####################################################################################################> >####################################################################################################@item> is: [{:quantity=>"\000''\0009\0008\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000\0002\000''\000",> :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > :number=>"\000''\0007\0004\000''\000", > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > :description=>"\000''\000S\000o\000m\000e\000 > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > \000h\000e\000r\000e\000!\000!\000''\000", >:category_id=>"\000''\000W\000N\000''\000"}]####################################################################################################> > > Can anyone kindly tell me why am I getting this kind of string instead of > simple string I entered in my csv file? And because of this it''s not being > saved into the table too, I have tried all possible formats but nothing > seems to be working. I want "Uploaded Item Number 1" instead of > "\000''\0006\0007\000''\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > -- > > Please consider the environment before printing this email. > > Regards, > Surya > > -- > You received this message because you are subscribed to the Google Groups"Ruby on Rails: Core" group.> To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email torubyonrails-core+unsubscribe@googlegroups.com.> For more options, visit this group athttp://groups.google.com/group/rubyonrails-core?hl=en.>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Matt Jones
2011-Mar-23 17:45 UTC
Re: Problem in saving fields to database from csv using fastercsv
On Mar 23, 2011, at 9:42 AM, Surya wrote:> Hi, > > First of all very sorry for this long post. I am trying to save my csv data to table items which is associated with Item model. > > This is what my csv have: > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; ''description'';''cost_price'';''selling_price'' > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description here!!'';''780'';''890'' > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description here!!'';''8660'';''9790'' > > First row show the fields for items table. > > Here I am using fastercsv to process my csv and paperclip to upload. > > I am able to process file read content and able to fill up the field too here is the processing code: > > def proc_csv > @import = Import.find(params[:id]) > @lines = parse_csv_file(@import.csv.path) > @lines.shiftAs others have pointed out, this isn''t the right place for this. In any case, I suspect your issue is that Excel has spit out a UTF-16 file and you didn''t tell whatever''s parsing it about that... --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Frederick Cheung
2011-Mar-23 18:50 UTC
Re: Problem in saving fields to database from csv using fastercsv
On 23 Mar 2011, at 13:42, Surya <raj.surya19-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > First of all very sorry for this long post. I am trying to save my csv data to table items which is associated with Item model.It looks like the data you are importing UTF16 data. I''m guessing you were expecting UTF8 - I''d start by reading up on the tools available for converting between string encodings in ruby (1.8 and 1.9 are quite different in this respect) Fred> > This is what my csv have: > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; ''description'';''cost_price'';''selling_price'' > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description here!!'';''780'';''890'' > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description here!!'';''8660'';''9790'' > > First row show the fields for items table. > > Here I am using fastercsv to process my csv and paperclip to upload. > > I am able to process file read content and able to fill up the field too here is the processing code: > > def proc_csv > @import = Import.find(params[:id]) > @lines = parse_csv_file(@import.csv.path) > @lines.shift > @lines.each do |line , j| > unless line.nil? > line_split = line.split(";") > unless ((line_split[0].nil?) or (line_split[1].nil?) or (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or (line_split[5].nil?)) > # I used puts to get to know about what''s going on. > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > @item = [:name => line_split[0], :number => line_split[1], :sub_category_id => line_split[2],:category_id => line_split[3],:quantity => line_split[4], :sku => line_split[5], :description => line_split[6], :cost_price => line_split[7], :selling_price => line_split[8]] > puts "#"*100+"@item is: #{@item.inspect}"+"#"*100 > > end > end > end > redirect_to import_path(@import) > end > > but the problem is that when it process it and when I check the @item in console it looks like this: > ####################################################################################################@item is: [{:quantity=>"\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", :cost_price=>"\000''\0007\0008\0000\000''\000", :number=>"\000''\0005\0004\000''\000", :selling_price=>"\000''\0008\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### > > ####################################################################################################@item is: [{:quantity=>"\000''\0009\0008\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0002\000''\000", :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", :cost_price=>"\000''\0008\0006\0006\0000\000''\000", :number=>"\000''\0007\0004\000''\000", :selling_price=>"\000''\0009\0007\0009\0000\000''\000", :sub_category_id=>"\000''\000K\000R\000T\000''\000", :description=>"\000''\000S\000o\000m\000e\000 \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 \000h\000e\000r\000e\000!\000!\000''\000", :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### > > > Can anyone kindly tell me why am I getting this kind of string instead of simple string I entered in my csv file? And because of this it''s not being saved into the table too, I have tried all possible formats but nothing seems to be working. I want "Uploaded Item Number 1" instead of "\000''\0006\0007\000''\000", :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > -- > Please consider the environment before printing this email. > > > Regards, > Surya > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Surya
2011-Mar-24 05:20 UTC
Re: Problem in saving fields to database from csv using fastercsv
Ok, Thanks for letting me know about this. On Wed, Mar 23, 2011 at 11:15 PM, Matt Jones <al2o3cr@gmail.com> wrote:> > On Mar 23, 2011, at 9:42 AM, Surya wrote: > > > Hi, > > > > First of all very sorry for this long post. I am trying to save my csv > data to table items which is associated with Item model. > > > > This is what my csv have: > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > ''description'';''cost_price'';''selling_price'' > > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some > Description here!!'';''780'';''890'' > > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some > Description here!!'';''8660'';''9790'' > > > > First row show the fields for items table. > > > > Here I am using fastercsv to process my csv and paperclip to upload. > > > > I am able to process file read content and able to fill up the field too > here is the processing code: > > > > def proc_csv > > @import = Import.find(params[:id]) > > @lines = parse_csv_file(@import.csv.path) > > @lines.shift > > As others have pointed out, this isn''t the right place for this. In any > case, I suspect your issue is that Excel has spit out a UTF-16 file and you > didn''t tell whatever''s parsing it about that... > > --Matt Jones > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > >-- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Surya
2011-Mar-24 05:22 UTC
Re: Problem in saving fields to database from csv using fastercsv
So what could be the possible solution? I wasted my whole day trying to figure out the issue. But didn''t get anything related with this :( On Thu, Mar 24, 2011 at 12:20 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 23 Mar 2011, at 13:42, Surya <raj.surya19-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > First of all very sorry for this long post. I am trying to save my csv data > to table items which is associated with Item model. > > > It looks like the data you are importing UTF16 data. I''m guessing you were > expecting UTF8 - I''d start by reading up on the tools available for > converting between string encodings in ruby (1.8 and 1.9 are quite different > in this respect) > > Fred > > > This is what my csv have: > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > ''description'';''cost_price'';''selling_price'' > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description > here!!'';''780'';''890'' > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description > here!!'';''8660'';''9790'' > > > First row show the fields for items table. > > Here I am using fastercsv to process my csv and paperclip to upload. > > I am able to process file read content and able to fill up the field too > here is the processing code: > > def proc_csv > @import = Import.find(params[:id]) > @lines = parse_csv_file(@import.csv.path) > @lines.shift > @lines.each do |line , j| > unless line.nil? > line_split = line.split(";") > unless ((line_split[0].nil?) or (line_split[1].nil?) or > (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or > (line_split[5].nil?)) > # I used puts to get to know about what''s going on. > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > @item = [:name => line_split[0], :number => line_split[1], > :sub_category_id => line_split[2],:category_id => line_split[3],:quantity => > line_split[4], :sku => line_split[5], :description => line_split[6], > :cost_price => line_split[7], :selling_price => line_split[8]] > puts "#"*100+"@item is: #{@item.inspect}"+"#"*100 > > end > end > end > redirect_to import_path(@import) > end > > > but the problem is that when it process it and when I check the @item in > console it looks like this: > > ####################################################################################################@item > is: [{:quantity=>"\000''\0006\0007\000''\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000", > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > :cost_price=>"\000''\0007\0008\0000\000''\000", > :number=>"\000''\0005\0004\000''\000", > :selling_price=>"\000''\0008\0009\0000\000''\000", > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > :description=>"\000''\000S\000o\000m\000e\000 > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > \000h\000e\000r\000e\000!\000!\000''\000", > :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### > > ####################################################################################################@item > is: [{:quantity=>"\000''\0009\0008\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0002\000''\000", > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > :number=>"\000''\0007\0004\000''\000", > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > :description=>"\000''\000S\000o\000m\000e\000 > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > \000h\000e\000r\000e\000!\000!\000''\000", > :category_id=>"\000''\000W\000N\000''\000"}]#################################################################################################### > > > Can anyone kindly tell me why am I getting this kind of string instead of > simple string I entered in my csv file? And because of this it''s not being > saved into the table too, I have tried all possible formats but nothing > seems to be working. I want "Uploaded Item Number 1" instead > of "\000''\0006\0007\000''\000", > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > -- > > Please consider the environment before printing this email. > > Regards, > Surya > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-24 08:04 UTC
Re: Problem in saving fields to database from csv using fastercsv
On Mar 24, 5:22 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So what could be the possible solution? I wasted my whole day trying to > figure out the issue. But didn''t get anything related with this :( >convert the text to utf8 before you try and parse it. Fred> On Thu, Mar 24, 2011 at 12:20 AM, Frederick Cheung < > > > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 23 Mar 2011, at 13:42, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > First of all very sorry for this long post. I am trying to save my csv data > > to table items which is associated with Item model. > > > It looks like the data you are importing UTF16 data. I''m guessing you were > > expecting UTF8 - I''d start by reading up on the tools available for > > converting between string encodings in ruby (1.8 and 1.9 are quite different > > in this respect) > > > Fred > > > This is what my csv have: > > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > > ''description'';''cost_price'';''selling_price'' > > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some Description > > here!!'';''780'';''890'' > > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some Description > > here!!'';''8660'';''9790'' > > > First row show the fields for items table. > > > Here I am using fastercsv to process my csv and paperclip to upload. > > > I am able to process file read content and able to fill up the field too > > here is the processing code: > > > def proc_csv > > @import = Import.find(params[:id]) > > @lines = parse_csv_file(@import.csv.path) > > @lines.shift > > @lines.each do |line , j| > > unless line.nil? > > line_split = line.split(";") > > unless ((line_split[0].nil?) or (line_split[1].nil?) or > > (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or > > (line_split[5].nil?)) > > # I used puts to get to know about what''s going on. > > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > > @item = [:name => line_split[0], :number => line_split[1], > > :sub_category_id => line_split[2],:category_id => line_split[3],:quantity => > > line_split[4], :sku => line_split[5], :description => line_split[6], > > :cost_price => line_split[7], :selling_price => line_split[8]] > > puts "#"*100+"@item is: #...@item.inspect}"+"#"*100 > > > end > > end > > end > > redirect_to import_path(@import) > > end > > > but the problem is that when it process it and when I check the @item in > > console it looks like this: > > > ########################################################################### #########################@item > > is: [{:quantity=>"\000''\0006\0007\000''\000", > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0001\000''\000", > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > > :cost_price=>"\000''\0007\0008\0000\000''\000", > > :number=>"\000''\0005\0004\000''\000", > > :selling_price=>"\000''\0008\0009\0000\000''\000", > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > :description=>"\000''\000S\000o\000m\000e\000 > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > \000h\000e\000r\000e\000!\000!\000''\000", > > :category_id=>"\000''\000W\000N\000''\000"}]################################# ################################################################### > > > ########################################################################### #########################@item > > is: [{:quantity=>"\000''\0009\0008\000", > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 \0002\000''\000", > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > > :number=>"\000''\0007\0004\000''\000", > > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > :description=>"\000''\000S\000o\000m\000e\000 > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > \000h\000e\000r\000e\000!\000!\000''\000", > > :category_id=>"\000''\000W\000N\000''\000"}]################################# ################################################################### > > > Can anyone kindly tell me why am I getting this kind of string instead of > > simple string I entered in my csv file? And because of this it''s not being > > saved into the table too, I have tried all possible formats but nothing > > seems to be working. I want "Uploaded Item Number 1" instead > > of "\000''\0006\0007\000''\000", > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > > -- > > > Please consider the environment before printing this email. > > > Regards, > > Surya > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > > Please consider the environment before printing this email. > > Regards, > Surya-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Surya
2011-Mar-25 04:56 UTC
Re: Re: Problem in saving fields to database from csv using fastercsv
Hi Thanks for your kind response, Frederick. I have finally sort it out. It was my csv file which was in UTF-61le format, I made a new file with UITF-8 format and that worked as the way I wanted. One more thing I wants to ask that is, I am getting the string now in this format - :name => "''Uploaded item number 1''" instead of :name => "Uploaded item number 1", and because of this the field that is being saved in the column name is: ''Uploaded item number 1'' instead of Uploaded item number 1. how do I remove this quote ( '' ) ? Any idea? On Thu, Mar 24, 2011 at 1:34 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Mar 24, 5:22 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > So what could be the possible solution? I wasted my whole day trying to > > figure out the issue. But didn''t get anything related with this :( > > > convert the text to utf8 before you try and parse it. > > Fred > > On Thu, Mar 24, 2011 at 12:20 AM, Frederick Cheung < > > > > > > > > > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On 23 Mar 2011, at 13:42, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi, > > > > > First of all very sorry for this long post. I am trying to save my csv > data > > > to table items which is associated with Item model. > > > > > It looks like the data you are importing UTF16 data. I''m guessing you > were > > > expecting UTF8 - I''d start by reading up on the tools available for > > > converting between string encodings in ruby (1.8 and 1.9 are quite > different > > > in this respect) > > > > > Fred > > > > > This is what my csv have: > > > > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > > > ''description'';''cost_price'';''selling_price'' > > > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some > Description > > > here!!'';''780'';''890'' > > > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some > Description > > > here!!'';''8660'';''9790'' > > > > > First row show the fields for items table. > > > > > Here I am using fastercsv to process my csv and paperclip to upload. > > > > > I am able to process file read content and able to fill up the field > too > > > here is the processing code: > > > > > def proc_csv > > > @import = Import.find(params[:id]) > > > @lines = parse_csv_file(@import.csv.path) > > > @lines.shift > > > @lines.each do |line , j| > > > unless line.nil? > > > line_split = line.split(";") > > > unless ((line_split[0].nil?) or (line_split[1].nil?) or > > > (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or > > > (line_split[5].nil?)) > > > # I used puts to get to know about what''s going on. > > > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > > > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > > > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > > > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > > > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > > > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > > > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > > > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > > > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > > > > @item = [:name => line_split[0], :number => line_split[1], > > > :sub_category_id => line_split[2],:category_id => > line_split[3],:quantity => > > > line_split[4], :sku => line_split[5], :description => line_split[6], > > > :cost_price => line_split[7], :selling_price => line_split[8]] > > > puts "#"*100+"@item is: #...@item.inspect}"+"#"*100 > > > > > end > > > end > > > end > > > redirect_to import_path(@import) > > > end > > > > > but the problem is that when it process it and when I check the @item > in > > > console it looks like this: > > > > > > ########################################################################### > #########################@item > > > is: [{:quantity=>"\000''\0006\0007\000''\000", > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > \0001\000''\000", > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > > > :cost_price=>"\000''\0007\0008\0000\000''\000", > > > :number=>"\000''\0005\0004\000''\000", > > > :selling_price=>"\000''\0008\0009\0000\000''\000", > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > :description=>"\000''\000S\000o\000m\000e\000 > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > ################################################################### > > > > > > ########################################################################### > #########################@item > > > is: [{:quantity=>"\000''\0009\0008\000", > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > \0002\000''\000", > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > > > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > > > :number=>"\000''\0007\0004\000''\000", > > > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > :description=>"\000''\000S\000o\000m\000e\000 > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > ################################################################### > > > > > Can anyone kindly tell me why am I getting this kind of string instead > of > > > simple string I entered in my csv file? And because of this it''s not > being > > > saved into the table too, I have tried all possible formats but nothing > > > seems to be working. I want "Uploaded Item Number 1" instead > > > of "\000''\0006\0007\000''\000", > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > > \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > > > > -- > > > > > Please consider the environment before printing this email. > > > > > Regards, > > > Surya > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > > Please consider the environment before printing this email. > > > > Regards, > > Surya > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-25 22:02 UTC
Re: Problem in saving fields to database from csv using fastercsv
On Mar 25, 4:56 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > Thanks for your kind response, Frederick. I have finally sort it out. It was > my csv file which was in UTF-61le format, I made a new file with UITF-8 > format and that worked as the way I wanted. > > One more thing I wants to ask that is, I am getting the string now in > this format - :name => "''Uploaded item number 1''" instead of :name => > "Uploaded item number 1", and because of this the field that is being saved > in the column name is: ''Uploaded item number 1'' instead of Uploaded item > number 1. how do I remove this quote ( '' ) ? Any idea?You can probably tell FasterCSV that your fields are quoted with '' rather than the more usual " Fred> > On Thu, Mar 24, 2011 at 1:34 PM, Frederick Cheung < > > > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Mar 24, 5:22 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > So what could be the possible solution? I wasted my whole day trying to > > > figure out the issue. But didn''t get anything related with this :( > > > convert the text to utf8 before you try and parse it. > > > Fred > > > On Thu, Mar 24, 2011 at 12:20 AM, Frederick Cheung < > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On 23 Mar 2011, at 13:42, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi, > > > > > First of all very sorry for this long post. I am trying to save my csv > > data > > > > to table items which is associated with Item model. > > > > > It looks like the data you are importing UTF16 data. I''m guessing you > > were > > > > expecting UTF8 - I''d start by reading up on the tools available for > > > > converting between string encodings in ruby (1.8 and 1.9 are quite > > different > > > > in this respect) > > > > > Fred > > > > > This is what my csv have: > > > > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > > > > ''description'';''cost_price'';''selling_price'' > > > > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some > > Description > > > > here!!'';''780'';''890'' > > > > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some > > Description > > > > here!!'';''8660'';''9790'' > > > > > First row show the fields for items table. > > > > > Here I am using fastercsv to process my csv and paperclip to upload. > > > > > I am able to process file read content and able to fill up the field > > too > > > > here is the processing code: > > > > > def proc_csv > > > > @import = Import.find(params[:id]) > > > > @lines = parse_csv_file(@import.csv.path) > > > > @lines.shift > > > > @lines.each do |line , j| > > > > unless line.nil? > > > > line_split = line.split(";") > > > > unless ((line_split[0].nil?) or (line_split[1].nil?) or > > > > (line_split[2].nil?) or (line_split[3].nil?) or (line_split[4].nil?) or > > > > (line_split[5].nil?)) > > > > # I used puts to get to know about what''s going on. > > > > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > > > > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > > > > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > > > > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > > > > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > > > > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > > > > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > > > > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > > > > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > > > > @item = [:name => line_split[0], :number => line_split[1], > > > > :sub_category_id => line_split[2],:category_id => > > line_split[3],:quantity => > > > > line_split[4], :sku => line_split[5], :description => line_split[6], > > > > :cost_price => line_split[7], :selling_price => line_split[8]] > > > > puts "#"*100+"@item is: #...@item.inspect}"+"#"*100 > > > > > end > > > > end > > > > end > > > > redirect_to import_path(@import) > > > > end > > > > > but the problem is that when it process it and when I check the @item > > in > > > > console it looks like this: > > > ########################################################################### > > #########################@item > > > > is: [{:quantity=>"\000''\0006\0007\000''\000", > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > \0001\000''\000", > > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > > > > :cost_price=>"\000''\0007\0008\0000\000''\000", > > > > :number=>"\000''\0005\0004\000''\000", > > > > :selling_price=>"\000''\0008\0009\0000\000''\000", > > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > > :description=>"\000''\000S\000o\000m\000e\000 > > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > > ################################################################### > > > ########################################################################### > > #########################@item > > > > is: [{:quantity=>"\000''\0009\0008\000", > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > \0002\000''\000", > > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > > > > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > > > > :number=>"\000''\0007\0004\000''\000", > > > > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > > :description=>"\000''\000S\000o\000m\000e\000 > > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > > ################################################################### > > > > > Can anyone kindly tell me why am I getting this kind of string instead > > of > > > > simple string I entered in my csv file? And because of this it''s not > > being > > > > saved into the table too, I have tried all possible formats but nothing > > > > seems to be working. I want "Uploaded Item Number 1" instead > > > > of "\000''\0006\0007\000''\000", > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > > > \0001\000''\000" . Any help will be appreciated. Thanks in advance :) > > > > > -- > > > > > Please consider the environment before printing this email. > > > > > Regards, > > > > Surya > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > > Please consider the environment before printing this email. > > > > Regards, > > > Surya > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > > Please consider the environment before printing this email. > > Regards, > Surya-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Surya
2011-Mar-26 05:12 UTC
Re: Re: Problem in saving fields to database from csv using fastercsv
Thanks Frederick, I got it :) I did this: FasterCSV.foreach(path_to_csv, :col_sep => '';'', :quote_char => "''") do |row| Used row here! end And it removed '' from the string. On Sat, Mar 26, 2011 at 3:32 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Mar 25, 4:56 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi > > > > Thanks for your kind response, Frederick. I have finally sort it out. It > was > > my csv file which was in UTF-61le format, I made a new file with UITF-8 > > format and that worked as the way I wanted. > > > > One more thing I wants to ask that is, I am getting the string now > in > > this format - :name => "''Uploaded item number 1''" instead of :name => > > "Uploaded item number 1", and because of this the field that is being > saved > > in the column name is: ''Uploaded item number 1'' instead of Uploaded item > > number 1. how do I remove this quote ( '' ) ? Any idea? > > You can probably tell FasterCSV that your fields are quoted with '' > rather than the more usual " > > Fred > > > > On Thu, Mar 24, 2011 at 1:34 PM, Frederick Cheung < > > > > > > > > > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Mar 24, 5:22 am, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > So what could be the possible solution? I wasted my whole day trying > to > > > > figure out the issue. But didn''t get anything related with this :( > > > > > convert the text to utf8 before you try and parse it. > > > > > Fred > > > > On Thu, Mar 24, 2011 at 12:20 AM, Frederick Cheung < > > > > > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On 23 Mar 2011, at 13:42, Surya <raj.sury...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi, > > > > > > > First of all very sorry for this long post. I am trying to save my > csv > > > data > > > > > to table items which is associated with Item model. > > > > > > > It looks like the data you are importing UTF16 data. I''m guessing > you > > > were > > > > > expecting UTF8 - I''d start by reading up on the tools available for > > > > > converting between string encodings in ruby (1.8 and 1.9 are quite > > > different > > > > > in this respect) > > > > > > > Fred > > > > > > > This is what my csv have: > > > > > > > ''name'';''number'';''sub_category_id'';''category_id'';''quantity'';''sku''; > > > > > ''description'';''cost_price'';''selling_price'' > > > > > ''Uploaded Item Number 1'';''54'';''KRT'';''WN'';''67'';''WNKRT0054'';''Some > > > Description > > > > > here!!'';''780'';''890'' > > > > > ''Uploaded Item Number 2'';''74'';''KRT'';''WN'';''98;''WNKRT0074'';''Some > > > Description > > > > > here!!'';''8660'';''9790'' > > > > > > > First row show the fields for items table. > > > > > > > Here I am using fastercsv to process my csv and paperclip to > upload. > > > > > > > I am able to process file read content and able to fill up the > field > > > too > > > > > here is the processing code: > > > > > > > def proc_csv > > > > > @import = Import.find(params[:id]) > > > > > @lines = parse_csv_file(@import.csv.path) > > > > > @lines.shift > > > > > @lines.each do |line , j| > > > > > unless line.nil? > > > > > line_split = line.split(";") > > > > > unless ((line_split[0].nil?) or (line_split[1].nil?) or > > > > > (line_split[2].nil?) or (line_split[3].nil?) or > (line_split[4].nil?) or > > > > > (line_split[5].nil?)) > > > > > # I used puts to get to know about what''s going on. > > > > > puts "*"*50+"line_split[0]: #{line_split[0]}"+"*"*50 > > > > > puts "*"*50+"line_split[1]: #{line_split[1]}"+"*"*50 > > > > > puts "*"*50+"line_split[2]: #{line_split[2]}"+"*"*50 > > > > > puts "*"*50+"line_split[3]: #{line_split[3]}"+"*"*50 > > > > > puts "*"*50+"line_split[4]: #{line_split[4]}"+"*"*50 > > > > > puts "*"*50+"line_split[5]: #{line_split[5]}"+"*"*50 > > > > > puts "*"*50+"line_split[6]: #{line_split[6]}"+"*"*50 > > > > > puts "*"*50+"line_split[7]: #{line_split[7]}"+"*"*50 > > > > > puts "*"*50+"line_split[8]: #{line_split[8]}"+"*"*50 > > > > > > > @item = [:name => line_split[0], :number => line_split[1], > > > > > :sub_category_id => line_split[2],:category_id => > > > line_split[3],:quantity => > > > > > line_split[4], :sku => line_split[5], :description => > line_split[6], > > > > > :cost_price => line_split[7], :selling_price => line_split[8]] > > > > > puts "#"*100+"@item is: #...@item.inspect}"+"#"*100 > > > > > > > end > > > > > end > > > > > end > > > > > redirect_to import_path(@import) > > > > > end > > > > > > > but the problem is that when it process it and when I check the > @item > > > in > > > > > console it looks like this: > > > > > > ########################################################################### > > > #########################@item > > > > > is: [{:quantity=>"\000''\0006\0007\000''\000", > > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > > \0001\000''\000", > > > > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0005\0004\000''\000", > > > > > :cost_price=>"\000''\0007\0008\0000\000''\000", > > > > > :number=>"\000''\0005\0004\000''\000", > > > > > :selling_price=>"\000''\0008\0009\0000\000''\000", > > > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > > > :description=>"\000''\000S\000o\000m\000e\000 > > > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > > > ################################################################### > > > > > > ########################################################################### > > > #########################@item > > > > > is: [{:quantity=>"\000''\0009\0008\000", > > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > > \0002\000''\000", > > > > > > :sku=>"\000''\000W\000N\000K\000R\000T\0000\0000\0007\0004\000''\000", > > > > > :cost_price=>"\000''\0008\0006\0006\0000\000''\000", > > > > > :number=>"\000''\0007\0004\000''\000", > > > > > :selling_price=>"\000''\0009\0007\0009\0000\000''\000", > > > > > :sub_category_id=>"\000''\000K\000R\000T\000''\000", > > > > > :description=>"\000''\000S\000o\000m\000e\000 > > > > > \000D\000e\000s\000c\000r\000i\000p\000t\000i\000o\000n\000 > > > > > \000h\000e\000r\000e\000!\000!\000''\000", > > > > > > :category_id=>"\000''\000W\000N\000''\000"}]################################# > > > ################################################################### > > > > > > > Can anyone kindly tell me why am I getting this kind of string > instead > > > of > > > > > simple string I entered in my csv file? And because of this it''s > not > > > being > > > > > saved into the table too, I have tried all possible formats but > nothing > > > > > seems to be working. I want "Uploaded Item Number 1" instead > > > > > of "\000''\0006\0007\000''\000", > > > > > :name=>"\000''\000U\000p\000l\000o\000a\000d\000e\000d\000 > > > > > \000I\000t\000e\000m\000 \000N\000u\000m\000b\000e\000r\000 > > > > > \0001\000''\000" . Any help will be appreciated. Thanks in advance > :) > > > > > > > -- > > > > > > > Please consider the environment before printing this email. > > > > > > > Regards, > > > > > Surya > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > -- > > > > > > Please consider the environment before printing this email. > > > > > > Regards, > > > > Surya > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > > Please consider the environment before printing this email. > > > > Regards, > > Surya > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Please consider the environment before printing this email. Regards, Surya -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.