2012-12-26 2 views
3

가능한 중복 : 내 스크립트를 실행할 때
ruby 1.9: invalid byte sequence in UTF-8루비 잘못된 바이트 시퀀스 (하면 ArgumentError)

내가 현재 파일 시스템 크롤러를 구축하고 다음과 같은 오류 메시지가 표시됨 :

wordcrawler.rb:8:in `block in <main>': invalid byte sequence in UTF-8 (ArgumentError) 
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:41:in `block in find' 
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `catch' 
    from /Users/Anconia/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `find' 
    from wordcrawler.rb:5:in `<main>' 

그리고 여기 내 코드입니다 :

,
require 'find' 

count = 0 

Find.find('/Users/Anconia/') do |file|     # '/' for root directory on OS X 
    if file =~ /\b(\.txt|\.doc|\.docx)\b/    # check if filename ends in desired format 
    contents = File.read(file) 
     if contents =~ /regex/ 
     puts file 
     count += 1 
    end 
    end 
end 

puts "#{count} files were found" 

내 dev 환경에서 나는 1.9.3을 사용했다; 그러나 루비 1.8.7로 전환하면 스크립트가 제대로 실행됩니다. 그리고 가능하면 1.9.3을 계속 사용하고 싶습니다. 나는이 게시물 (ruby 1.9: invalid byte sequence in UTF-8)의 모든 솔루션을 시도했지만 내 문제는 여전히 지속됩니다. 어떤 제안?

+0

@SeanHill의 구현 예로 사용할 수 있습니다, 해당 게시물 이전 – Anconia

+0

을 연구하고있다 코드는 여기에 솔루션을 포함하지 않는 것 // 유래. com/a/8873922/367611 –

+0

@SeanHill correct – Anconia

답변

5

위 게시물의 내용을 제대로 이해하지 못했습니다. HTTP : 최소한으로이 내가 언급 한 바와 같이 this post

require 'find' 

count = 0 

Find.find('/Users/Anconia/') do |file|            # '/' for root directory on OS X 
    if file =~ /\b(\.txt|\.doc|\.docx)\b/           # check if filename ends in desired format 
    contents = File.read(file).encode!('UTF-8', 'UTF-8', :invalid => :replace) # resolves encoding errors - must use 1.9.3 else use iconv 
     if contents =~ /regex/ 
     puts file 
     count += 1 
    end 
    end 
end 

puts "#{count} files were found" 
+0

답변 해 주셔서 감사합니다. 나는 다음을 사용하여 해결 된 문제를 가지고있다 : invalid => : replace :-) – Arkan

관련 문제