Removing whitespaces in a CSV file

后端 未结 3 984
猫巷女王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:49

    @prices = CSV.parse(IO.read('prices.csv'), :headers=>true, 
       :header_converters=> lambda {|f| f.strip},
       :converters=> lambda {|f| f ? f.strip : nil})
    

    The nil test is added to the row but not header converters assuming that the headers are never nil, while the data might be, and nil doesn't have a strip method. I'm really surprised that, AFAIK, :strip is not a pre-defined converter!

提交回复
热议问题