Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby CSV:忽略空列_Ruby_String_Csv - Fatal编程技术网

Ruby CSV:忽略空列

Ruby CSV:忽略空列,ruby,string,csv,Ruby,String,Csv,当列为空时,是否可以忽略或替换为空字符串? 我的CSV如下所示: "DE","Klasse","Deutsch", "x" "EN","Class","Carpenter", "DE","Klasse","Mathe", ,,, 因此,并非所有列都已填充。有几个空列。它返回了一个错误: TypeError: no implicit conversion of nil into String 我所做的是: csv_contents = CSV.read("path_to_csv", optio

当列为空时,是否可以忽略或替换为空字符串? 我的CSV如下所示:

"DE","Klasse","Deutsch", "x"
"EN","Class","Carpenter",
"DE","Klasse","Mathe",
,,,
因此,并非所有列都已填充。有几个空列。它返回了一个错误:

TypeError: no implicit conversion of nil into String
我所做的是:

csv_contents = CSV.read("path_to_csv", options)
str=["local, type, name"]
csv_contents.each_with_index do |row, i|
if row[3]==nil
  str << row[0] + ", " + row[1] + ", " + row[2] 
end
end
csv\u contents=csv.read(“路径到csv”,选项)
str=[“本地,类型,名称”]
csv_contents.each_with_index do |行,i|
如果第[3]行==nil

str您可以添加一个方法来检查值是否为nil。如果是,请返回“”

然后,在代码中使用它:

str << get_row(row, 0) + ", " + get_row(row, 1) + ", " + get_row(row, 2)
str
当列为空时,是否可以忽略或替换为空字符串

当你有一行像
foo,,bar,
,这意味着那些列是空白的。是的,你是对的,但是空白表示那些已经是空字符串
)。这里没有必要将空字符串替换为空字符串:-)。无论如何,当您将这些数据保存到输出文件中时,显然可以忽略它们

因为应该删除最后一列中包含“x”的所有行

毫无疑问,这是可能的

以下是我将如何做到这一点:

require 'csv'

input_file_path = File.expand_path('input.csv',File.dirname(__FILE__))
output_file_path = File.expand_path('output.csv',File.dirname(__FILE__)) 

option = { :skip_blanks => true, 
           :quote_char => "\'", 
           :converters => lambda do |field|
             field.to_s.tr('"','')
           end
         }

CSV.open(output_file_path,'w',:force_quotes => true) do |out_row|
  CSV.foreach(input_file_path, option) do |in_row|
    row_to_add = in_row.reject(&:empty?)
    unless row_to_add.empty?
      row_to_add.last[/^\s*x$/i] ? out_row.puts(row_to_add[0..-2]) : out_row.puts(row_to_add)
    end
  end
end
输出

"DE","Klasse","Deutsch"
"EN","Class","Carpenter"
"DE","Klasse","Mathe"

您是如何收集
csv\u内容的
,同时显示该部分。@ArupRakshit好的。我会做好的away@catwoman如果这个答案对你有帮助,你应该考虑接受它。当然,我会的。我以前做不到,不得不等8分钟@Abdo@Abdo也来看看我的:-)
"DE","Klasse","Deutsch"
"EN","Class","Carpenter"
"DE","Klasse","Mathe"