I want to write values parsed from a CSV file to a YAML file:
home_phone = row['HomePhone']
if home_phone.length == 10
puts "First Name: #{row['first_Name']} - Home Phone: #{home_phone} - Zip Code: #{zip_code}"
elsif home_phone.length == 11 && home_phone.start_with?('1')
home_phone.slice!(0)
puts "First Name: #{row['first_Name']} - Home Phone: #{home_phone} - Zip Code: #{zip_code}"
else
puts "First Name: #{row['first_Name']} - Home Phone: #{"0000000000"} - Zip Code: #{zip_code}"
end
The above code is used for printing the details row-wise. The output obtained from the code is:
First Name: Douglas - Home Phone: 4252745000 - Zip Code: 50309'
'First Name: Aya - Home Phone: 9995901339 - Zip Code: 90210'
'First Name: Audrey - Home Phone: 0 - Zip Code: 05667'
How can I write these values to a YAML file?
If you want to convert a CSV file to YAML…
I created a file called “test.csv” which looks like:
Using this code:
Which generates:
I get this output from
ppshowing a correct round-trip of the CSV data through YAML-land:This is just a simple solution, which loads the entire CSV file into memory. That’s not a scalable solution if you are processing files bigger than the available RAM on your system. CSV supports
foreachwhich will let you read the file line by line, but YAML might find it difficult to write the file line-by-line because of how it generates the file; It expects a complete hash or array, but you’ll be passing it sub-arrays, which will result in a YAML file looking like:Loading that with
YAML.load_filewill result in:[ [0] [ [0] "First Name", [1] "Home Phone", [2] "Zip Code" ] ]In other words, only the first row will be returned.
The workaround is to use
YAML.load_documents(File.open('test.yaml', 'r'))instead ofload_file, which reassembles the parts into one array again.