【发布时间】:2017-12-04 08:40:30
【问题描述】:
我试图在我的 python 3 终端中读取一个 6GB 的文件,但无法执行读取文件行。代码如下:
#define data directory
data_dir = 'C://Star/star_data/csv\Globe'
#read the review dataset
yelp = pd.read_csv(data_dir+'\star_data_python.csv')
X, y = star.data, star.target
X.shape
错误:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-4-bc09b45c73bb> in <module>()
4
5 #read the review dataset
----> 6 yelp = pd.read_csv(data_dir+'\star_data_python.csv')
7 X, y = star.data, star.target
8 X.shape
可能是什么问题?谢谢
【问题讨论】:
-
您在路径中同时使用了
/和\...如果您使用的是Windows,请仅在字符串前面使用/和r,例如data_dir = r'C://Star/star_data/csv/Globe' -
如果不是路径问题,请尝试将
,encoding='utf-8'添加到read_csv -
#define data directory 我已将其更正为下面的行,但我仍然遇到相同的错误。 data_dir = r'C://Star/star_data/csv/Globe' #读取评论数据集 yelp = pd.read_csv(data_dir + '/star_data_python.csv')
-
嗨@MugB,尝试将编码utf-8添加到您的csv文件中:
pd.read_csv(data_dir+'\star_data_python.csv, encoding='utf-8') -
pandas\parser.pyx in pandas.parser.TextReader.__cinit__ (pandas\parser.c:6086)() pandas\parser.pyx in pandas.parser.TextReader._get_header (pandas\parser. c:9266)() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 2543: invalid continuation byte
标签: python python-3.x