Home > database >  With ruby, how do I format a CSV correctly under it's respective header/column?
With ruby, how do I format a CSV correctly under it's respective header/column?

Time:10-13

In my previous question, I had a main CSV file and I had two extra CSVs. I wanted to go through both of these CSVs and remove the entries from the main CSV if something matched within these two files. My solution ended up looking like this:

require 'csv'

main_list = CSV.table('./script/main.csv', headers: true)
already_called = CSV.table('./script/already_called.csv', headers: true)
already_mailed = CSV.table('./script/already_mailed.csv', headers: true)

updated_list = main_list.delete_if do |main_row|
    already_called.any? { |called_row| main_row[:id] == called_row[:id] }
    already_mailed.any? { |mailed| main_row[:curr_addr_line_1] == mailed[:street_address_1] }
end

CSV.open('./script/final_list.csv', 'w', headers: main_list.headers, write_headers: true) do |writer|
    writer << updated_list
end

I believe this works, and now I want to write to final_list.csv with the data matching the headers, but this is not the case.

My ideal format is something along the lines of

id,nm,last_nm,first_nm,birth_dt, ......
"10001","PERSON, PersonA","PERSON","PersonA","5/5/1999"......
"10031","PERSON, PersonB","PERSON","PersonB","3/1/1901"......

However with my code above, I get something like this:

# redacted some headers
# headers seem to be fine?
id,nm,last_nm,first_nm,birth_dt, ......
"1003984,""PERSON, PersonA"",PersonA,PERSON,5/5/1999,58,MALE,ENGLISH,,
XXX-XXX-XXXX,[email protected],PO BOX ???,,CITY,STATE,ANOTHER PLACE,UNITED STATES,
???,""???"",4082000,INSURANCE,2,INSURANCE,""408,200,001.00"",MEDICARE A AND B,
""11,043,785.00"",2500084,ADDRESS,1,COMMERCIAL,
""27,083.00"",ADDRESS,I50.32,SYMPTOMS,???,DATE,OFFICE VISIT,""PERSON"",6/18/2021,""PERSON"",
0,,0,0,,,,""PERSON"",1,55.0,10/7/2020,,,
""[""""???"""",""""???"""",""""???""""]"",""[""""diag""""]"",
""[""""MED"""",""""MED"""",""""MED"""",""""MEDS"""",""""XYZ MED"""",
""""XYZ MEDS"""",""""MEDS"""",""""MEDIS"""",""""XYZ MEDS"""""",,,,,,

............ and more

I've redacted a lot of information above but I wanted to show the format I was getting. When I upload this to Google Sheets, it all outputs into one row with the headers being in the correct place, at least. However, it seems my entire output is just in one very long row. What's especially confusing are the long quotes at the end.

How do I match up the headers and columns correctly?

I've looked around for a while and I'm unsure how to proceed. Ruby is not my language of choice so I really appreciate some guidance!

CodePudding user response:

Thanks to @Stephan I ended up with this and it worked!

CSV.open('./script/final_list.csv', 'w', headers: main_list.headers, write_headers: true) do |writer|
    updated_list.each { |row| writer << row }
end
  • Related