Tag: csv

使用CSV类解析Ruby中的.csv文件

我正在使用Ruby 1.9.3并且我发现了CSV类,但我无法使用它。 基本上,我希望能够操作CSV的各种选项,然后将.csv文件拉入数组以进行操作,最终将该数组推回到新文件中。 这就是我目前所拥有的: require ‘csv’ CSV_Definition = CSV.New(:header_converters => :symbol) CSV_Total = CSV.Read(File.Path(“C:\Scripts\SQL_Log_0.csv”)) 但是,我不认为这是更改:header_converters的正确方法。 目前我无法让IRB解析这些代码片段(我不确定如何在IRB中require ‘csv’ )所以我没有任何特定的错误消息。 我对此的期望是创建一个数组(CSV_Total),其中包含一个没有符号的标题。 下一步是将该数组放回新文件中。 基本上它会擦除CSV文件。

我如何逐行使用Ruby将csv转换为数组?

输入文件如下: 狗,白,男性 猫,紫,女 大鼠,灰色,男性 我希望逐行完成并对数据做一些事情。 File.open(“animals.csv”) while file has next line currentline = array with each cell being an entry in the array if currentline[0] == dog put “dogs are cool” end put “your animal is a ” + currentline[0] end 你明白了,对吗? 我想用ifs和whatnot操纵数据线,并在最后打印出来。 谢谢

Ruby – 在内存中创建文件

无论如何在Ruby中编写以下代码而不将文件写入磁盘? temp_file = ‘path/to/file.csv’ users = [a@bc, c@ba] CSV.open(temp_file, “w”) do |csv| csv << data_for_report end Reports.sendreport users temp_file File.delete(temp_file) Reports.sendreport附加一个文件并发送一封电子邮件,因此它需要是一个文件……

如何正确写入CSV文件

我正在使用ruby 1.9.2并且还使用它的csv库。我想正确编写csv 像这样 name,country_code,destination,code Afghanistan,93,Bamain,51 Afghanistan,93,Bamain,52 Afghanistan,93,Bamain,53 Afghanistan,93,Parwan,91 我的代码是这样的 def export_data @coun = Country.all(:limit => 10) header = “name,country_code,destination,code” file = “my_file.csv” File.open(file, “w”) do |csv| csv << header @coun.each do |c| csv << [c.name, c.country_code, c.user_id, c.subscriber_id] # How puts line break here end end send_file(file) end 我上面提到了如何把我的行放在CSV文件中,并省略了这个叹息 用CSV“[]”覆盖每一行 Like [“Finland”,1,1,2334] 提前致谢..

阅读时Ruby CSV UTF8编码错误

这就是我在做的事情: csv = CSV.open(file_name, “r”) 我用这个来测试: line = csv.shift while not line.nil? puts line line = csv.shift end 我碰到了这个: ArgumentError: invalid byte sequence in UTF-8 我在这里读到答案 ,这就是我的尝试 csv = CSV.open(file_name, “r”, encoding: “windows-1251:utf-8”) 我遇到了以下错误: Encoding::UndefinedConversionError: “\x98″ to UTF-8 in conversion from Windows-1251 to UTF-8 然后我遇到了一个Ruby gem – charlock_holmes。 我想我会尝试使用它来查找源编码。 CharlockHolmes::EncodingDetector.detect(File.read(file_name)) => {:type=>:text, :encoding=>”windows-1252″, :confidence=>37, :language=>”fr”} […]

删除CSV文件中的空格

我有一个额外的空格字符串: First,Last,Email ,Mobile Phone ,Company,Title ,Street,City,State,Zip,Country, Birthday,Gender ,Contact Type 我想解析这一行并删除空格。 我的代码看起来像: namespace :db do task :populate_contacts_csv => :environment do require ‘csv’ csv_text = File.read(‘file_upload_example.csv’) csv = CSV.parse(csv_text, :headers => true) csv.each do |row| puts “First Name: #{row[‘First’]} \nLast Name: #{row[‘Last’]} \nEmail: #{row[‘Email’]}” end end end

Ruby:如何检测/智能猜测CSV文件中使用的分隔符?

我需要能够在我的Ruby项目中找出csv文件(逗号,空格或分号)中使用的分隔符。 我知道,csv模块中的Python中有一个Sniffer类可用于猜测给定文件的分隔符。 Ruby中有类似的东西吗? 非常感谢任何forms的帮助或想法。

在Ruby中读取CSV时如何跳过标题行?

Ruby的CSV类可以很容易地遍历每一行: CSV.foreach(file) { |row| puts row } 但是,这总是包含标题行,所以我将得到输出: header1, header2 foo, bar baz, yak 我不想要标题。 现在,当我打电话给… CSV.foreach(file, :headers => true) 我得到这个结果: # 当然,因为文档说: 此设置使#shift将行返回为CSV :: Row对象而不是Arrays 但是, 如何跳过标题行,将行作为简单数组返回? 我不希望返回复杂的CSV::Row对象。 我绝对不想这样做: first = true CSV.foreach(file) do |row| if first puts row first = false else # code for other rows end end

从Rails 3.2应用程序下载流式CSV

我迫切希望在我的Rails 3.2.2应用程序中使用流式CSV下载。 我已经尝试了’csv_builder’gem(https://github.com/dasil003/csv_builder),它宣传支持这个function,但似乎Rails 3.2中有一些变化使它无法工作(它产生了一个应用启动时出现“未初始化的常量ActionView :: Template :: Handler”错误。 还有其他任何想法或解决方案吗? 谢谢! 编辑:为了澄清,我需要将模型的所有条目导出为CSV文件。 有很多行,它是超时的…因此需要流式传输。 我过去曾使用过逗号gem(https://github.com/crafterm/comma),但此刻它也不支持流式传输。

如何使用ruby gsub Regexp与许多匹配?

我的csv文件内容在引用文本中有双引号 test,first,line,”you are a “kind” man”,thanks again,second,li,”my “boss” is you”,good 我需要用“”替换逗号前面或后面的每个双引号。 test,first,line,”you are a “”kind”” man”,thanks again,second,li,”my “”boss”” is you”,good 所以“被”替换为“” 我试过了 x.gsub(/([^,])”([^,])/, “#{$1}\”\”#{$2}”) 但没有奏效