Ruby on Rails里,如何迭代CSV文件实现高效数据处理?

2026-04-11 16:241阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计525个文字,预计阅读时间需要3分钟。

Ruby on Rails里,如何迭代CSV文件实现高效数据处理?

在Ruby中,你可以使用内置的库来格式化CSV文件。以下是一个简单的示例,展示了如何将CSV数据格式化并打印出来:

rubyrequire 'csv'

假设你有一个CSV文件路径csv_file_path='path_to_your_csv_file.csv'

读取CSV文件csv_data=CSV.read(csv_file_path, headers: true)

格式化并打印CSV数据csv_data.each do |row| puts row.to_csvend

这段代码首先引入了`csv`库,然后读取一个CSV文件,并使用`headers: true`参数来获取表头。接着,它遍历每一行数据,并使用`to_csv`方法将其格式化为CSV格式的字符串,然后打印出来。

请注意,你需要将`'path_to_your_csv_file.csv'`替换为你的CSV文件的实际路径。

我想在 ruby中打印出一个CSV文件,但我想将其格式化.有没有办法在层次结构意义上格式化数据?这是我需要经历的列表的一小部分:

Ruby on Rails里,如何迭代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

本文共计525个文字,预计阅读时间需要3分钟。

Ruby on Rails里,如何迭代CSV文件实现高效数据处理?

在Ruby中,你可以使用内置的库来格式化CSV文件。以下是一个简单的示例,展示了如何将CSV数据格式化并打印出来:

rubyrequire 'csv'

假设你有一个CSV文件路径csv_file_path='path_to_your_csv_file.csv'

读取CSV文件csv_data=CSV.read(csv_file_path, headers: true)

格式化并打印CSV数据csv_data.each do |row| puts row.to_csvend

这段代码首先引入了`csv`库,然后读取一个CSV文件,并使用`headers: true`参数来获取表头。接着,它遍历每一行数据,并使用`to_csv`方法将其格式化为CSV格式的字符串,然后打印出来。

请注意,你需要将`'path_to_your_csv_file.csv'`替换为你的CSV文件的实际路径。

我想在 ruby中打印出一个CSV文件,但我想将其格式化.有没有办法在层次结构意义上格式化数据?这是我需要经历的列表的一小部分:

Ruby on Rails里,如何迭代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