【问题标题】:Python utf8 codec can't decode byte 0x80 in position 103:invalid start bytePython utf8 编解码器无法解码位置 103 中的字节 0x80:无效的起始字节
【发布时间】: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-encoding tag info page 以获取故障排除提示,或者查看edit 您的问题以向我们展示错误周围的实际字节数。
  • 问号在您期望撇号的位置表明数据是错误的,甚至在您将其写入 CSV 文件之前。你的 Python 脚本拒绝处理不正确的输入数据是完全正确的。

标签: python utf-8 decode opencsv


【解决方案1】:

这是unicode转换错误,试试这个...

h = pd.read_csv('C:/Users/Desktop/Result.csv', encoding = "utf-8")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2023-03-09
    • 2020-09-22
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多