开发者

Call iconv from Ruby through system()

开发者 https://www.devze.com 2022-12-28 12:51 出处:网络
I have a problem with iconv tool. I try to call it from rake file in that way: Dir.glob(\"*.txt\") do |file|

I have a problem with iconv tool. I try to call it from rake file in that way:

Dir.glob("*.txt") do |file|
  system("iconv -f UTF-8 -t 'ASCII//TRANSLIT' #{ file } >> ascii_#{ file }")
end

But one file is converted partly (size of partialy converted: 10059092 bytes, before convertion: 10081854). Comparing this two files 开发者_JAVA技巧prove that not all content was writen to ASCII. When I call this command explicit from shell it works perfectly. Other smaller files are converted without problems. Is there any limitations on iconv or Ruby's system()?


It is always a good idea to check the return value of system to determine whether it was successful.

Dir.glob("*.txt") do |file|
  system("iconv -f UTF-8 -t 'ASCII//TRANSLIT' #{file} >> ascii_#{file}") or
    puts "iconv failed for file #{file}: #{$?}"
end

You could also try using the Iconv standard library, and thus get rid of the system call:

require 'iconv'

source_file = 'utf8.txt'
target_file = 'ascii.txt'

File.open(target_file, 'w') do |file|
  File.open(source_file).each_line do |line|
    file.write Iconv.conv('ASCII//TRANSLIT', 'UTF-8', line)
  end
end

with appropriate error checking added.

0

精彩评论

暂无评论...
验证码 换一张
取 消