python UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa6 in position 67
问题
读取TXT文件时报错: UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa6 in position 67
具体报错python语句是
file= open(r'E:\***\***\***\***\***\***\{0}\{1}_tweets.txt'.format(tid, uid))
all = file.readlines()
解决
这是因为你的编码方式不对,txt是utf-8编码,所以编码需要转为utf-8。即代码修改为
file= open(r'E:\***\***\***\***\***\***\{0}\{1}_tweets.txt'.format(tweet_id, user_id),encoding='utf-8')
all = file.readlines()