【发布时间】:2018-09-03 09:02:42
【问题描述】:
这是我的代码和我的 csv 文件的示例,在我运行代码后,错误显示 utf8 编解码器无法解码位置 103 的字节 0x80:无效的起始字节 如果有人可以帮忙
import csv
import pandas as pd
import numpy as np
import os
import sys
h = pd.read_csv('C:/Users/Desktop/Result.csv')
sentences = h['Mention']
def check_it(sentences):
if 'camera' in sentences:
return "Camera"
if 'Camera' in sentences:
return "Camera"
if 'display' in sentences:
return "Display"
if 'Display' in sentences:
return "Display"
if 'battery' in sentences:
return "Battery"
if 'Battery' in sentences:
return "Battery"
if 'temperature' in sentences:
return "Temperature"
if 'Temperature' in sentences:
return "Temperature"
if 'memory' in sentences:
return "Memory"
if 'Memory' in sentences:
return "Memory"
if 'audio' in sentences:
return "Audio"
if 'Audio' in sentences:
return "Audio"
if 'design' in sentences:
return "Design"
if 'Design' in sentences:
return "Design"
return "Others"
h.loc[:, 'Category'] = h.Mention.apply(check_it)
h.to_csv('C:/Desktop/ResultWithCategory.csv')
我在这段代码中遇到了错误,我不知道为什么会出现错误
【问题讨论】:
-
Excel 工作表的屏幕截图并不能真正揭示您尝试导入的 CSV 文件中有哪些字节。请参阅Stack Overflow
character-encodingtag info page 以获取故障排除提示,或者查看edit 您的问题以向我们展示错误周围的实际字节数。 -
问号在您期望撇号的位置表明数据是错误的,甚至在您将其写入 CSV 文件之前。你的 Python 脚本拒绝处理不正确的输入数据是完全正确的。
标签: python utf-8 decode opencsv