【问题标题】:pd.read_excel throws UnicodeDecodeErrorpd.read_excel 抛出 UnicodeDecodeError
【发布时间】:2023-03-18 21:26:01
【问题描述】:

我正在尝试将数据从 excel 读取到 pandas。我得到的文件来自api并且没有保存(访问文件需要特殊权限,所以我不想保存它)。当我尝试从文件中读取 excel 时

with open('path_to_file') as file:
    re = pd.read_excel(file)

我得到了错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x98 in position
10: invalid start byte

当我在文件位置输入路径时,一切正常

re = pd.read_excel('path-to-exactly-the-same-file')

有没有办法通过pandas读取excel而不保存和输入路径?

【问题讨论】:

  • 尝试将文件读取为字节:with open('path_to_file', 'rb') as file

标签: python pandas file


【解决方案1】:

缺少的部分是 'rb' in open

with open('path_to_file', 'rb') as file:
    re = pd.read_excel(file)

将文件视为二进制文件。想法取自error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2014-10-24
    • 1970-01-01
    • 2016-06-15
    • 2014-10-23
    相关资源
    最近更新 更多