简体   繁体   English

如何在ruby 1.8.7中读取CSV?

[英]how to read CSV in ruby 1.8.7?

I just want to read a csv file and discard the header row. 我只想读取一个csv文件并丢弃标题行。

When I do csv = CSV.read('file_name.csv','r') I get 当我做csv = CSV.read('file_name.csv','r')

.rvm/rubies/ruby-1.8.7-head/lib/ruby/1.8/csv.rb:98:in `read': can't convert String into Integer (TypeError) .rvm / rubies / ruby​​-1.8.7-head / lib / ruby​​ / 1.8 / csv.rb:98:在`read'中:无法将String转换为Integer(TypeError)

When I do 当我做

csv = CSV.open('file_name.csv','r')
csv.shift

I get 我懂了

.rvm/rubies/ruby-1.8.7-head/lib/ruby/1.8/csv.rb:639:in `get_row': CSV::IllegalFormatError (CSV::IllegalFormatError) from .rvm/rubies/ruby-1.8.7-head/lib/ruby/1.8/csv.rb:567:in `shift' .rvm / rubies / ruby​​-1.8.7-head / lib / ruby​​ / 1.8 / csv.rb:639:在`get_row'中:.rvm / rubies / ruby​​-1.8中的CSV :: IllegalFormatError(CSV :: IllegalFormatError)。 7头/lib/ruby/1.8/csv.rb:567:在'shift'中

What is an example of successfully reading csv files and manipulating data in ruby 1.8.7? 在ruby 1.8.7中成功读取csv文件并处理数据的示例是什么?


edit: 编辑:

first few lines of file_name.csv file_name.csv的前几行

Request, Target
http://www.asdf.com/,http://www.asdf.com/
http://www.asdf.com/_blank,http://www.asdf.com/
"http://www.asdf.com/,",http://www.asdf.com/

Do it like this: 像这样做:

csv = CSV.read('file_name.csv')

"r" is not a correct parameter for CSV.read "r"不是CSV.read的正确参数

The file line endings was the issue. 文件行结尾是问题所在。 I changed all of them to unix's LF. 我将它们全部更改为UNIX的LF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM