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/regex/18.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-将一列中包含的excel数据分离为单独的列_Ruby_Regex_Csv_Delimiter_Comma - Fatal编程技术网

Ruby-将一列中包含的excel数据分离为单独的列

Ruby-将一列中包含的excel数据分离为单独的列,ruby,regex,csv,delimiter,comma,Ruby,Regex,Csv,Delimiter,Comma,我试图使用Ruby来处理一些excel数据,但是我得到的.csv文件将所有数据都放在一列中 数据的标题和值用逗号分隔,但它们包含在第一列中。此外,第一列中的某些值的文本由引号包围,引号内有逗号 有没有办法用Ruby将第一列中的数据分隔成单独的列 我知道您可以在excel中执行此操作,但我希望能够在Ruby中执行此操作,这样我就不必手动更正每个.csv文件 我在下面提供了一个.csv文件的示例 所需的输出将是: {:header 1 => integer, :header 2 => t

我试图使用Ruby来处理一些excel数据,但是我得到的.csv文件将所有数据都放在一列中

数据的标题和值用逗号分隔,但它们包含在第一列中。此外,第一列中的某些值的文本由引号包围,引号内有逗号

有没有办法用Ruby将第一列中的数据分隔成单独的列

我知道您可以在excel中执行此操作,但我希望能够在Ruby中执行此操作,这样我就不必手动更正每个.csv文件

我在下面提供了一个.csv文件的示例

所需的输出将是:

{:header 1 => integer,
:header 2 => text,
:header 3 => "this text, has a comma within the quote"
:header 4 => integer} 

我感谢你的帮助

这里有一个简单的方法:

require 'csv'                                                                                                                                           
result = []                                                                       

csv = CSV.read('./file.csv')                                                      
headers = csv.shift                                                               
csv.each do |l|                                                                   
  hash = {}                                                                       
  hash[headers[0]] = l[0]                                                         
  hash[headers[1]] = l[1]                                                         
  hash[headers[2]] = l[2]                                                         
  hash[headers[3]] = l[3]                                                         
  result << hash                                                                  
end                                                                               

p result

[{"header 1"=>"integer",
  "header 2"=>"text",
  "header 3"=>"this text, has a comma within the quote",
  "header 4"=>"integer"},
 {"header 1"=>"integer",
  "header 2"=>"text",
  "header 3"=>"this text, has a comma within the quote",
  "header 4"=>"integer"}]
当然,这假设每行有4个值

编辑:以下是将结果实际写入文件的示例:

CSV.open('./output.csv', 'wb') do |csv|                                           
  result.each do |hash|                                                           
    temp = []                                                                     
    hash.each do |key, value|                                                     
      temp << "#{key} => #{value}"                                                
    end                                                                           
  csv << temp                                                                   
  end                                                                             
end   

谢谢我仍然得到一个带有哈希数组的输出,但是键值对是:[{row 1=>row 2},{row 1=>row 3}]我仍然在创建列时遇到问题。还有其他想法吗?我刚才也检查了一下,它看起来像是因为.csv文件在一列中包含数据,该列中的所有内容在本例中的第一行被引号包围:[标题1,标题2,标题3,标题4],这就是为什么它在关联单个值和链接到下面的行时遇到问题的原因。