首页 > 解决方案 > Python SyntaxError:非 ASCII 字符

问题描述

我的熊猫数据框中有一个字符串列。当我尝试将此列导出到 Excel 文件时,我遇到了错误。如果我删除该列,我能够导出数据框。

我试图解码该字符串列中的所有值。我收到以下错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position 16: ordinal not in range(128)

我调试了一下,在那个特定的列中找到了一个列值。我无法解码这个值('Einen Use case fr Brems-')。

我采用了该值并尝试了以下代码,再次遇到错误。

#!/usr/bin/python
# -*- coding: utf-8 -*-

text = 'Einen Use case fr Brems-'

print text.decode()

1)我也尝试添加以下行。

import sys  

reload(sys)  
sys.setdefaultencoding('utf8')

2)我在我的系统中试过这个:

import sys
print sys.getdefaultencoding()

我得到'ascii'作为输出。

标签: pythonpandaspython-unicodeunicode-string

解决方案


推荐阅读