意外的':'和意外'''错误

时间:2014-07-14 20:31:04

标签: ruby

我在ruby版本1.8.7中运行以下代码

require 'csv'

FILENAME = 'filename'

cleansed_output = File.open('filename_cleaned.csv','wb')

CSV.open(FILENAME, encoding: 'ISO8859-1') do |output|
  row_number = 1
  loop do
    begin
      break unless row = output.shift
      cleansed_output << row.to_csv
    rescue CSV::MalformedCSVError => e
      puts "Malformed row #: #{row_number}"
    ensure
      puts "Row number: #{row_number}" if row_number % 10000 == 0
      row_number += 1
    end
  end
end

cleansed_output.close

我收到以下错误:

csv_cleanser2.rb:7: syntax error, unexpected ':', expecting ')'
CSV.open(FILENAME, encoding: 'ISO8859-1') do |output|
                            ^
csv_cleanser2.rb:7: syntax error, unexpected ')', expecting $end
CSV.open(FILENAME, encoding: 'ISO8859-1') do |output|

我试图用破坏的分隔符清理csv。有人知道这里有什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

您正在使用ruby 1.8.7,但您尝试使用1.9哈希语法:

CSV.open(FILENAME, encoding: 'ISO8859-1') do |output|

尝试使用旧式哈希火箭:

CSV.open(FILENAME, :encoding => 'ISO8859-1') do |output|