
我有一个CSV文件,第一行的日期和第二行的标题,所以我需要能够跳过第一行迭代它.我尝试使用切片,但将CSV转换为数组,我真的想将其读作CSV,以便我可以利用标头.
解决方法 我不认为有一种优雅的方式,但它可以做到:require "csv"# Create a stream using the original file.# Don't use `textmode` since it generates a problem when using this approach.file = file.open "file.csv"# Consume the first CSV row.# `\r` is my row separator character. Verify your file to see if it's the same one.loop { break if file.readchar == "\r" }# Create your CSV object using the remainder of the stream.csv = CSV.new file,headers: true 总结 以上是内存溢出为你收集整理的ruby – 如何跳过CSV文件的第一行并将第二行作为标题全部内容,希望文章能够帮你解决ruby – 如何跳过CSV文件的第一行并将第二行作为标题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)