5

There is an array, how correctly to deduce in csv a file?

arr1 = [["A","B"], ["C","D"], ["E","F"], ["G","H"]]

Expected result in csv:

A,B
C,D
E,F
G,H

I do so:

out_file = File.open('file.csv', 'w')
arr1.each_index do |inx|
  arr1[inx].each do |val|
    out_file.puts val
  end
end

But, Prints all in one column:

A
B
C
D
..

If you output to the console through p val, then in each value is / r:

"A\r"
"B\r"
"C\r"
"D\r"

What do I do wrong?

Edit: result csv Excel result csv Vim

9
  • File.write('file.csv', [["A","B"], ["C","D"], ["E","F"], ["G","H"]].map { |e| e.join(",") }.join($/)). Commented Mar 16, 2017 at 10:17
  • @Andrey Deineko , Output all values in one cell Commented Mar 16, 2017 at 10:18
  • @DenisDerevyankin I don't think that Output all values in one cell aligns with the expected result you have provided. Commented Mar 16, 2017 at 10:24
  • @mudasobwa Again in one column, but now with (",") in front Commented Mar 16, 2017 at 10:29
  • I do not follow. The code I provided produces exactly what you have requested. Commented Mar 16, 2017 at 10:46

3 Answers 3

6

You are not writing to file.

require 'csv'
CSV.open('file.csv', 'w') do |csv|
  arr1.each { |ar| csv << ar }
end
Sign up to request clarification or add additional context in comments.

6 Comments

<<': undefined method map' for #<String:0x00000002405ea0>
@DenisDerevyankin I edited the answer, there's no longer map method
This doesn't work. CSV#<< takes either a CSV::Row or an Array, but you are passing a String. CSV#<< will then try to map the elements to CSV, but will raise a NoMethodError: undefined method `map' for "A, B":String.
Work, but one column A ,B ,C ,D Can be used for each value of chomp ?
@DenisDerevyankin I do not understand what your comment means.
|
4

If all you want is to simply print out the CSV string, then you can do it like this:

csv_string = CSV.generate { |csv| array2d.each { |row| csv << row }

Here's a worked example:

> # array2d contains the raw data
> csv_string = CSV.generate { |csv| array2d.each { |row| csv << row } }
> puts csv_string
5014,"John O""Neill",4295,1,Finance Plus
314,"Thomas, Duncan",436,2,Finance Plus
1930,Fraser Smith,436,12,Finance Plus
5057,Fred McDonald,436,12,Finance Plus

Note that it handles double-quotes and commas inside strings.

See: https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html

Comments

0

It works,

require 'csv'
CSV.open('file.csv', 'w') do |csv|
  arr1.each { |ar| csv << ar }
end

But it was necessary to finish in front of it:

 arr1.each_index do |inx|
    arr1[inx].each do |val|
    val.chop!    
  end
 end

To delete a line break \r

and it works:

File.write('file.csv', [["A","B"], ["C","D"], ["E","F"], ["G","H"]].map { |e| e.join(",") }.join($/))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.