2012-06-18 62 views
1

我试图浏览目录中的每个.xls文件,并为每个文件保存一个.csv文件。我正在使用'roo'宝石,并且看到一些错误。完全开放其他方法,我只需要保持在Ruby而不移动到Ruby on Rails。我正在运行Ruby 1.9.2并在Mac上执行终端。Ruby将Excel另存为CSV

到目前为止的代码:

#! /usr/bin/ruby 

require 'roo' 


# change excel files to csv 
begin 
    Dir["/tmp/*.xls"].each do |file| 
     file_path = "#{file}" 
     file_basename = File.basename(file, ".xls") 
     xls = Excel.new(file_path) 
     xls.to_csv = ("/tmp/#{file_basename}.csv") 
    end 
end 

这里是我的错误:(行号是不一样的例子,第27行是开始语句)

/Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:284:in `platform_specific_iconv': uninitialized constant Excel::Iconv (NameError) 
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:277:in `normalize_string' 
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:139:in `block in sheets' 
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:139:in `collect' 
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:139:in `sheets' 
    from /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/roo-1.10.1/lib/roo/excel.rb:123:in `initialize' 
    from excel_conversion_test.rb:32:in `new' 
    from excel_conversion_test.rb:32:in `block in <main>' 
    from excel_conversion_test.rb:28:in `each' 
    from excel_conversion_test.rb:28:in `<main>' 

至于建议我尝试安装inconv并得到以下错误:

ERROR: Error installing iconv: 
    ERROR: Failed to build gem native extension. 

     /Users/pierce/.rbenv/versions/1.9.2-p290/bin/ruby extconf.rb 
checking for iconv() in iconv.h... no 
checking for iconv() in -liconv... yes 
checking for const of iconv() 2nd argument... no 
creating Makefile 

make 
/usr/bin/gcc-4.2 -I. -I/Users/pierce/.rbenv/versions/1.9.2-p290/include/ruby-1.9.1/x86_64-darwin11.2.0 -I/Users/pierce/.rbenv/versions/1.9.2-p290/include/ruby-1.9.1/ruby/backward -I/Users/pierce/.rbenv/versions/1.9.2-p290/include/ruby-1.9.1 -I. -I'/Users/pierce/.rbenv/versions/1.9.2-p290/include' -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -fno-common -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wshorten-64-to-32 -Wno-long-long -pipe -o iconv.o -c iconv.c 
In file included from iconv.c:21: 
/Users/pierce/.rbenv/versions/1.9.2-p290/include/ruby-1.9.1/ruby/backward/st.h:2:2: warning: #warning use "ruby/st.h" instead of bare "st.h" 
iconv.c:22:20: error: intern.h: No such file or directory 
iconv.c: In function ‘map_charset’: 
iconv.c:139: error: ‘struct RHash’ has no member named ‘tbl’ 
iconv.c:139: error: ‘struct RHash’ has no member named ‘tbl’ 
iconv.c:142: error: ‘struct RHash’ has no member named ‘tbl’ 
iconv.c: In function ‘iconv_create’: 
iconv.c:187: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:188: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:190: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:190: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:190: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:190: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:192: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:193: error: ‘struct RString’ has no member named ‘len’ 
iconv.c: In function ‘iconv_fail’: 
iconv.c:328: error: ‘struct RString’ has no member named ‘len’ 
iconv.c: In function ‘iconv_fail_retry’: 
iconv.c:352: error: ‘ruby_errinfo’ undeclared (first use in this function) 
iconv.c:352: error: (Each undeclared identifier is reported only once 
iconv.c:352: error: for each function it appears in.) 
iconv.c: In function ‘rb_str_derive’: 
iconv.c:371: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:371: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:373: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:373: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:374: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c: In function ‘iconv_convert’: 
iconv.c:418: error: ‘struct RArray’ has no member named ‘len’ 
iconv.c:418: error: ‘struct RArray’ has no member named ‘ptr’ 
iconv.c:436: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:437: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:455: warning: comparison of unsigned expression >= 0 is always true 
iconv.c:458: warning: comparison between signed and unsigned 
iconv.c:459: warning: comparison between signed and unsigned 
iconv.c:491: warning: implicit conversion shortens 64-bit value into a 32-bit value 
iconv.c:494: warning: implicit conversion shortens 64-bit value into a 32-bit value 
iconv.c:497: error: ‘struct RArray’ has no member named ‘len’ 
iconv.c:498: error: ‘struct RArray’ has no member named ‘ptr’ 
iconv.c:499: error: ‘struct RArray’ has no member named ‘ptr’ 
iconv.c:501: error: ‘struct RString’ has no member named ‘len’ 
iconv.c:502: error: ‘struct RString’ has no member named ‘ptr’ 
iconv.c:514: warning: implicit conversion shortens 64-bit value into a 32-bit value 
iconv.c: In function ‘iconv_s_convert’: 
iconv.c:615: error: ‘struct RString’ has no member named ‘len’ 
make: *** [iconv.o] Error 1 


Gem files will remain installed in /Users/pierce/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/iconv-0.1 for inspection. 

回答

4

添加此:require 'iconv'之前require 'roo'。我发现这个解决方案here,我在谷歌上使用这个查询:roo/excel.rb:284。一个例子here!关于该行=的通知:xls.to_csv = ("/tmp/#{file_basename}.csv")不应该在那里。

+0

很棒的建议。我试图安装iconv并收到很多错误(添加到原始问题)。从我所读的内容来看,它似乎是1.9之前的红宝石的修补程序。 – analyticsPierce

+1

再次查看我的代码,我看到'xls.to_csv'和'(“/tmp/#{file_basename}.csv”)'之间的“=”。请,你能删除它并尝试吗?对不起,看到只知道这个问题。希望它有效。 – David

+1

是的!谢谢你。如果我能阅读和拼写,我可能会是一个更好的开发者。 – analyticsPierce

2

我决定发布一个答案,试图解释这种情况,来自Google的任何其他人。

正如大卫指出,要解决这个错误...

uninitialized constant Excel::Iconv 

...你必须要求 “的iconv”:

require "iconv" 
require "roo" 

这是因为袋鼠宝石通话Iconv.new在其内部的Excel类,但Roo忘记要求“iconv”本身,所以你不得不这样做。这是一个错误。它与调用Set.new没有区别require "set"

Iconv是Ruby 1.8和1.9标准库的一部分。你不会安装它。它已经在那里。

但值得指出的是,Iconv在Ruby 1.9中已被弃用,并在Ruby 2.0中被删除。