Removing whitespaces in a CSV file

后端 未结 3 1137
猫巷女王i
猫巷女王i 2021-02-19 02:10

I have a string with extra whitespace:

First,Last,Email  ,Mobile Phone ,Company,Title  ,Street,City,State,Zip,Country, Birthday,Gender ,Contact Type
3条回答
  •  迷失自我
    2021-02-19 02:46

    You can strip your hash first:

    csv.each do |unstriped_row|
      row = {}
      unstriped_row.each{|k, v| row[k.strip] = v.strip}
      puts "First Name: #{row['First']} \nLast Name: #{row['Last']} \nEmail: #{row['Email']}"
    end
    

    Edited to strip hash keys too

提交回复
热议问题