Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:22:57+00:00 2026-06-17T19:22:57+00:00

I want to write values parsed from a CSV file to a YAML file:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:22:58+00:00Added an answer on June 17, 2026 at 7:22 pm

    If you want to convert a CSV file to YAML…

    I created a file called “test.csv” which looks like:

    First Name,Home Phone,Zip Code
    Douglas,4252745000,50309
    Aya,9995901339,90210
    Audrey,0,05667
    

    Using this code:

    require 'csv'
    require 'yaml'
    
    csv = CSV.read('test.csv')
    File.write('test.yaml', csv.to_yaml)
    pp YAML.load_file('test.yaml')
    

    Which generates:

    ---
    - - First Name
      - Home Phone
      - Zip Code
    - - Douglas
      - '4252745000'
      - '50309'
    - - Aya
      - '9995901339'
      - '90210'
    - - Audrey
      - '0'
      - '05667'
    

    I get this output from pp showing a correct round-trip of the CSV data through YAML-land:

    [["First Name", "Home Phone", "Zip Code"],
     ["Douglas", "4252745000", "50309"],
     ["Aya", "9995901339", "90210"],
     ["Audrey", "0", "05667"]]
    

    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 foreach which 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:

    ---
    - - First Name
      - Home Phone
      - Zip Code
    ---
    - - Douglas
      - '4252745000'
      - '50309'
    ---
    - - Aya
      - '9995901339'
      - '90210'
    ---
    - - Audrey
      - '0'
      - '05667'
    

    Loading that with YAML.load_file will 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 of load_file, which reassembles the parts into one array again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

helo, i want to read values from this file: http://www.nbp.pl/kursy/xml/lastC.xml and fill grid with
I want to combine combine fields from an input .csv file for output to
I want to write a class with three int values in them and manipulate
I want to write a TeX file in Stata which contains a p-value rounded
I want to modify (Remove all white spaces) all the values of Column Name
I want write a simple query which will fetch data from a table (which
I want to receive an array that contains all the h1 tag values from
parse_ini_file could be used to get configuration values from an ini file to use
I'm having a major problem with writing values into a new CSV file. I
I want to write a function which can validate a given value (passed as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.