使用fastercsv将字段从csv保存到数据库时出现问题

发布于 2024-10-25 14:45:14 字数 4085 浏览 2 评论 0原文

我正在尝试将 csv 数据保存到与项目模型关联的表项目中。

这就是我的 csv 的内容:

'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'

第一行显示项目表的字段。

在这里,我使用 fastcsv 来处理我的 csv 和回形针来上传。

我能够处理文件读取内容并能够填写该字段,这里是处理代码:

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

但问题是,当它处理它时以及当我检查控制台中的 @item 时,它看起来像这个:

####################################################################################################@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"}]####################################################################################################

谁能告诉我为什么我会得到这种字符串而不是我在 csv 文件中输入的简单字符串?因此,它也没有保存到 items 表中,我已经尝试了所有可能的格式,但似乎没有任何效果。我想要 :name => “上传的项目编号 1” 而不是 :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" 。任何帮助将不胜感激。提前致谢 :)

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 items table too, I have tried all possible formats but nothing seems to be working. I want :name => "Uploaded Item Number 1" instead of :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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-11-01 14:45:14

在我把头撞到墙上并对这个问题感到沮丧之后,我发现这是我的 CSV 文件格式不正确(UTF16le),当我用 UTF-8 编码制作另一个 csv 文件时,我取得突破。虽然我还剩下一个问题,那就是出现的字符串是这样的: :name => "'Uploaded Item number 1'" 因此,当它保存到数据库中时,该列包含的数据是: 'Uploaded Item number 1' 。你知道我该怎么做吗: :name => “上传的项目编号 1”

After punching my head on to the wall and getting frustrated with this issue, I figured out that it was my CSV file that was not in proper format (was in UTF16le) and when I made an another csv file in UTF-8 encoding, I get the break through. Though I'm left with one more issue which is the string that comes is like this: :name => "'Uploaded Item number 1'" So when it saves into the database the column contains the data is: 'Uploaded Item number 1' . Do you have any idea how can I do it to like this: :name => "Uploaded Item number 1" ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文