Home > Back-end >  Issue with writing .csv file with ruby
Issue with writing .csv file with ruby

Time:11-19

Im learning ruby and i have this issue: I'm having and issue with writing . CSV by a . Text file. That's my Ruby code:

require 'csv'

class Plantas <

    Struct.new(:identifier, :code, :lang, :fullname)
  
  end


f = File.open("planta.txt", "r")

f.each_line { |line|

  fields = line.split(',')

  newPlant = Plantas.new


  newPlant.identifier = fields[0].tr_s('"', '').strip
  newPlant.code = fields[2].tr_s('"', '').strip
  newPlant.lang = fields[3].tr_s('"', '').strip
  newPlant.fullname = fields[10].tr_s('"', '').strip


    str = "#{newPlant.identifier} #{newPlant.code} #{newPlant.lang} #{newPlant.fullname}" #linies a imprimir
    #puts str

        CSV.open("plantas.csv", "w") do |csv|
            csv << [str]
end
}  

And that's my .txt file:

"identifier","datatype","code","lang","langno","preferred","status","creation","modification","country","fullname","authority","shortname"
"N1952","PFL","LEECO","la","1","0","N","06/06/2000","09/03/2010","","Leea coccinea non","Planchon","Leea coccinea non"
"N51170","PFL","CAWUR","en","4","0","N","28/02/2002","24/08/2010","","Toddy palm","","Toddy palm"

And that's the output on the .csv

N51170 CAWUR en Toddy palm

Just write the last line instead of all the lines printed in the .txt I want that the code print me all the lines in the file.csv without ' " ' and ','

Could someone help? Thanks!

CodePudding user response:

You are very close!

Given:

% cat file.csv
"identifier","datatype","code","lang","langno","preferred","status","creation","modification","country","fullname","authority","shortname"
"N1952","PFL","LEECO","la","1","0","N","06/06/2000","09/03/2010","","Leea coccinea non","Planchon","Leea coccinea non"
"N51170","PFL","CAWUR","en","4","0","N","28/02/2002","24/08/2010","","Toddy palm","","Toddy palm"

You can modify your code this way:

require 'csv'

class Plantas <
        Struct.new(:identifier, :code, :lang, :fullname)
    end
    
f_in = File.open("/tmp/file.csv", "r")
f_out=CSV.open("/tmp/plantas.csv", "w")

f_in.each_line { |line|
    
    fields = line.split(',')
    
    newPlant = Plantas.new
    
    newPlant.identifier = fields[0].tr_s('"', '').strip
    newPlant.code = fields[2].tr_s('"', '').strip
    newPlant.lang = fields[3].tr_s('"', '').strip
    newPlant.fullname = fields[10].tr_s('"', '').strip
    
        str = "#{newPlant.identifier} #{newPlant.code} #{newPlant.lang} #{newPlant.fullname}" #linies a imprimir
        f_out << [str]
}  

Which results in this:

% cat plantas.csv
identifier code lang fullname
N1952 LEECO la Leea coccinea non
N51170 CAWUR en Toddy palm

Even Better you can correctly treat the input as CSV as read it as such:

# same top part, then:

f_in = CSV.open("/tmp/file.csv", "r")
f_out=CSV.open("/tmp/plantas.csv", "w")

f_in.each { |row|
    
    newPlant = Plantas.new
    
    newPlant.identifier = row[0]
    newPlant.code = row[2]
    newPlant.lang = row[3]
    newPlant.fullname = row[10]
    
    str = "#{newPlant.identifier} #{newPlant.code} #{newPlant.lang} #{newPlant.fullname}" #linies a imprimir
    puts str
    f_out << [str]
} 
  • Related