I have a problem with iconv tool. I try to call it from rake file in that way:
我有iconv工具的问题。我尝试以这种方式从rake文件中调用它:
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 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()?
但是一个文件被部分转换(部分转换的大小:10059092字节,转换前:10081854)。比较这两个文件证明并非所有内容都写入ASCII。当我从shell中显式调用此命令时,它可以完美地工作。其他较小的文件转换没有问题。 iconv或Ruby的系统()有任何限制吗?
1 个解决方案
#1
0
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:
您也可以尝试使用Iconv标准库,从而摆脱系统调用:
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.
添加了适当的错误检查。
#1
0
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:
您也可以尝试使用Iconv标准库,从而摆脱系统调用:
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.
添加了适当的错误检查。