我有一个通过FTP从大型机获取平面文件的进程。这通常适用于某些文件。在其他情况下,我得到:Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8
那就是使用Net::FTP's gettextfile方法。下面是我的代码:
def find_file( position, value ) # => Value = CLKDRP03.txt, Forget the variable Position
ftp = Net::FTP.new('IP') # => status 200
ftp.login('user','pass') # => True
files = ftp.list('*' + value + '*') # => Obtain the file
if files[0] != nil
str_file = files[0].split(" ").last # => Obtain the name of the file. In this case CLKDRP03.txt
ftp.gettextfile(str_file,'data/input/' + str_file) # => ERROR!, str_file = CLKDRP03.txt
# => data/input is the path where I put the files from FTP.
return str_file
else
return false
end
end如果我注释ftp.gettextfile行,错误就会一直出现。
提前感谢!对不起,我的英语不好。
发布于 2015-10-30 02:37:02
对于来自google的任何人来说,这就是我修复编码错误的方法。
在声明或打开文件之前,添加以下代码
.force_encoding("UTF-8")因此,在将文件声明为如下所示的变量之前:
csv_file = params[:file].read.force_encoding("UTF-8")我希望这能帮助一些人,因为我已经有这个问题很长一段时间了,但现在它工作了耶!
https://stackoverflow.com/questions/30494001
复制相似问题