UnicodeDecodeError: 'utf-8'编解码器无法解码位置 0 中的字节 0xff:Python 中的起始字节无效。虽然文件是用 utf-8 编码的
当文件已经是 utf-8 格式时,我在下面的代码中遇到解码错误。请解释我如何解决这个问题。 我在循环中遇到错误 对于读卡器中的行:
def loadPhishTank(self):
db = RedirectDB(self.runConfig)
phishFile = self.runConfig.phishTankLocation+'phishTank-'+self.runConfig.day+'.csv'
if (not os.path.exists(self.runConfig.phishTankLocation)):
os.makedirs(self.runConfig.phishTankLocation)
self.downloadPhishTank(phishFile)
with open(phishFile, encoding='utf-8') as fin:
reader = csv.DictReader(fin)
urls = {}
for row in reader:
url = row['url']
meta = {'phish_detail_url':row['phish_detail_url'],
'submission_time':row['submission_time'],
'src':'phishTank'}
urls[url] = meta
samples = random.sample(list(urls.keys()),self.runConfig.phishTankSampleSize)
for sample in samples:
db.addUrlsFromList(sample,urls[sample],'phishTank',self.runConfig)
db.close()
I am getting a decoding error in the below code while the file is already is in utf-8. Please explain how I can solve this issue.
I am getting errors on the loop
for row in reader:
def loadPhishTank(self):
db = RedirectDB(self.runConfig)
phishFile = self.runConfig.phishTankLocation+'phishTank-'+self.runConfig.day+'.csv'
if (not os.path.exists(self.runConfig.phishTankLocation)):
os.makedirs(self.runConfig.phishTankLocation)
self.downloadPhishTank(phishFile)
with open(phishFile, encoding='utf-8') as fin:
reader = csv.DictReader(fin)
urls = {}
for row in reader:
url = row['url']
meta = {'phish_detail_url':row['phish_detail_url'],
'submission_time':row['submission_time'],
'src':'phishTank'}
urls[url] = meta
samples = random.sample(list(urls.keys()),self.runConfig.phishTankSampleSize)
for sample in samples:
db.addUrlsFromList(sample,urls[sample],'phishTank',self.runConfig)
db.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论