I'm looking to print out a CSV file in ruby but I would like to make it formatted. Is there a way to Format the data in a hierarchy sense? here's a small portion of the list I need to go through :
,"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"
the code I have is :
require 'csv'
col_data = []
CSV.foreach("primary_NAICS_code.txt") {|row| col_data << row}
puts col_data
this just prints out everything. Is it an array with in an array ? something like:
CSV.foreach do |row|
row.each do |line|
puts line
end
end
any help would point me in the right direction.
I would like to get the information formatted to something like this:
|_ <~~ row 1 column 1
| |__<~ row 1 column 2
| | |__<~row 2 column 2
| | | |__
| | | | |__ etc...
| | | | | |__
Since your data is already indented, you just have to convert/format it. Something like this should work:
col_data.each do |row|
indentation, (text,*) = row.slice_before(String).to_a
puts indentation.fill("|").join(" ") + "_ " + text
end
Output:
|_ 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With