【发布时间】:2014-11-20 18:26:50
【问题描述】:
我有一个每天自动生成的大型远程文件。我无法控制文件的生成方式。我正在使用 Paramiko 打开文件,然后搜索它以查找给定行是否与文件中的行匹配。
但是,我收到以下错误:
UnicodeDecodeError:“utf8”编解码器无法解码位置 57 中的字节 0x96:无效起始字节
我的代码:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(host, username=user, password=pass)
self.sftp_client = self.ssh.open_sftp()
self.remote_file = self.sftp_client.open(filepath, mode='r')
def checkPhrase(self, phrase):
found = 0
self.remote_file.seek(0)
for line in self.remote_file:
if phrase in line:
found = 1
break
return found
我在这一行收到错误:for line in self.remote_file: 显然文件中有一个字符超出了 utf8 的范围。
有没有办法在读取时重新编码该行或简单地忽略错误?
【问题讨论】: