使用pd.read_table作为txt文件时出现SyntaxError :( unicode错误)

时间:2018-01-22 21:38:02

标签: python pandas unicode

我正在阅读带有Pandas模式的txt文件:

Alabama[edit]
Auburn (Auburn University)[1]
Florence (University of North Alabama)
Jacksonville (Jacksonville State University)[2]
Livingston (University of West Alabama)[2]
Montevallo (University of Montevallo)[2]
Troy (Troy University)[2]
Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4]
Tuskegee (Tuskegee University)[5]
Alaska[edit]
Fairbanks (University of Alaska Fairbanks)[2]
Arizona[edit]
Flagstaff (Northern Arizona University)[6]
Tempe (Arizona State University)
Tucson (University of Arizona)

由:

import pandas as pd
df = pd.read_table('file path', sep='\n', header=None) 

但是我收到了这个错误:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in ` 
enter code here`position 65-66: truncated \uXXXX escape

我真的不明白为什么。我真的很感激帮助。

1 个答案:

答案 0 :(得分:0)

您似乎需要设置正确的编码来加载文件。

尝试

df = pd.read_table('file path', sep='\n', header=None, encoding='utf-8') 

df = pd.read_table('file path', sep='\n', header=None, encoding='WINDOWS-1252')

如果它仍然不起作用,您可以使用chardet之类的包来检测文件的编码,然后加载它。