如何使用Ruby on Rails在Ruby中高效迭代处理CSV文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计557个文字,预计阅读时间需要3分钟。
在Ruby中,你可以使用CSV库来处理CSV文件,并对其进行格式化。以下是一个简化的示例,展示了如何读取CSV文件并格式化数据:
rubyrequire 'csv'
读取CSV文件csv_text=File.read('data.csv')csv=CSV.parse(csv_text, headers: true)
格式化数据formatted_csv=csv.map do |row| # 假设你需要根据某些条件格式化数据 formatted_row=row.dup formatted_row['11']=Agriculture, Forestry, Fishing and Hunting if row['11'].include?('11') formatted_rowend
将格式化后的数据写入新的CSV文件CSV.open('formatted_data.csv', 'w') do |csv_out| csv_out < 这段代码首先读取名为`data.csv`的CSV文件,然后遍历每一行,对需要格式化的字段进行修改,最后将格式化后的数据写入到名为`formatted_data.csv`的新文件中。 ,"11: Agriculture, Forestry, Fishing and Hunting",,
,,"111: Crop Production",
,,,"111110: Soybean Farming"
,,,"111120: Oilseed (except Soybean) Farming"
,,,"111130: Dry Pea and Bean Farming"
,,,"111140: Wheat Farming"
,,"112: Animal Production",
,,,"112111: Beef Cattle Ranching and Farming"
,,,"112112: Cattle Feedlots"
,,,"112120: Dairy Cattle and Milk Production"
,,,"112130: Dual-Purpose Cattle Ranching and Farming"
我的代码是: require 'csv'
col_data = []
CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row}
puts col_data
这只是打印出来的一切.它是一个数组中的数组吗?就像是: CSV.foreach do |row|
row.each do |line|
puts line
end
end
任何帮助都会指引我朝着正确的方向前进.
我想获取格式化为这样的信息:
|_ <~~ row 1 column 1 | |__<~ row 1 column 2 | | |__<~row 2 column 2 | | | |__ | | | | |__ etc... | | | | | |__ 由于您的数据已经缩进,您只需转换/格式化它.这样的事情应该有效:
col_data.each do |row| indentation, (text,*) = row.slice_before(String).to_a puts indentation.fill("|").join(" ") + "_ " + text end
输出:
|_ 11: Agriculture, Forestry, Fishing and Hunting | |_ 111: Crop Production | | |_ 111110: Soybean Farming | | |_ 111120: Oilseed (except Soybean) Farming | | |_ 111130: Dry Pea and Bean Farming | | |_ 111140: Wheat Farming | |_ 112: Animal Production | | |_ 112111: Beef Cattle Ranching and Farming | | |_ 112112: Cattle Feedlots | | |_ 112120: Dairy Cattle and Milk Production | | |_ 112130: Dual-Purpose Cattle Ranching and Farming
本文共计557个文字,预计阅读时间需要3分钟。
在Ruby中,你可以使用CSV库来处理CSV文件,并对其进行格式化。以下是一个简化的示例,展示了如何读取CSV文件并格式化数据:
rubyrequire 'csv'
读取CSV文件csv_text=File.read('data.csv')csv=CSV.parse(csv_text, headers: true)
格式化数据formatted_csv=csv.map do |row| # 假设你需要根据某些条件格式化数据 formatted_row=row.dup formatted_row['11']=Agriculture, Forestry, Fishing and Hunting if row['11'].include?('11') formatted_rowend
将格式化后的数据写入新的CSV文件CSV.open('formatted_data.csv', 'w') do |csv_out| csv_out < 这段代码首先读取名为`data.csv`的CSV文件,然后遍历每一行,对需要格式化的字段进行修改,最后将格式化后的数据写入到名为`formatted_data.csv`的新文件中。 ,"11: Agriculture, Forestry, Fishing and Hunting",,
,,"111: Crop Production",
,,,"111110: Soybean Farming"
,,,"111120: Oilseed (except Soybean) Farming"
,,,"111130: Dry Pea and Bean Farming"
,,,"111140: Wheat Farming"
,,"112: Animal Production",
,,,"112111: Beef Cattle Ranching and Farming"
,,,"112112: Cattle Feedlots"
,,,"112120: Dairy Cattle and Milk Production"
,,,"112130: Dual-Purpose Cattle Ranching and Farming"
我的代码是: require 'csv'
col_data = []
CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row}
puts col_data
这只是打印出来的一切.它是一个数组中的数组吗?就像是: CSV.foreach do |row|
row.each do |line|
puts line
end
end
任何帮助都会指引我朝着正确的方向前进.
我想获取格式化为这样的信息:
|_ <~~ row 1 column 1 | |__<~ row 1 column 2 | | |__<~row 2 column 2 | | | |__ | | | | |__ etc... | | | | | |__ 由于您的数据已经缩进,您只需转换/格式化它.这样的事情应该有效:
col_data.each do |row| indentation, (text,*) = row.slice_before(String).to_a puts indentation.fill("|").join(" ") + "_ " + text end
输出:
|_ 11: Agriculture, Forestry, Fishing and Hunting | |_ 111: Crop Production | | |_ 111110: Soybean Farming | | |_ 111120: Oilseed (except Soybean) Farming | | |_ 111130: Dry Pea and Bean Farming | | |_ 111140: Wheat Farming | |_ 112: Animal Production | | |_ 112111: Beef Cattle Ranching and Farming | | |_ 112112: Cattle Feedlots | | |_ 112120: Dairy Cattle and Milk Production | | |_ 112130: Dual-Purpose Cattle Ranching and Farming

