python - 从 CSV/List 读取时出现 UnicodeDecodeError:意外的数据结束
问题描述
所以我正在尝试使用一种叫做 DeepMoji 的东西来对一个满是推文的 csv 进行评分。推文必须以 Unicode 编码。我已经能够使它与一个小数据集一起工作,但是对于我拥有超过 200,000 个点的数据集,我收到了这个错误:UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 254: unexpected end of数据。
我尝试过的代码和解决方案如下,但给出了同样的错误,有人有什么想法吗?
TEST_SENTENCES = []
with open('Cleaned_Data3.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
TEST_SENTENCES.append(row["Tweet"])
try:
[x.encode('utf-8') for x in TEST_SENTENCES]
except:
for rows in TEST_SENTENCES: #attempt to fix the problem
str=unicode(str, errors='replace')
这是完整的错误代码。
Traceback (most recent call last):
File "C:\Users\pjame\Desktop\DeepMoji-master\examples\score_texts_emojis.py", line 24, in <module>
for row in reader:
File "C:\Python27\lib\site-packages\unicodecsv\py2.py", line 217, in next
row = csv.DictReader.next(self)
File "C:\Python27\lib\csv.py", line 108, in next
row = self.reader.next()
File "C:\Python27\lib\site-packages\unicodecsv\py2.py", line 128, in next
for value in row]
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 254: unexpected end of data
解决方案
推荐阅读
- c++ - 从c ++中执行的shell命令获取返回状态的安全方法是什么
- angular - Angular 6 POST 到 Amazon S3 错误 412 - FormData 中没有文件且编码类型不正确
- c# - 在 ASP.NET Core 中,从 Cookie 而不是 Headers 中读取 JWT 令牌
- polymer-3.x - 如何在聚合物 3.x 中将值传递给点击事件的函数?
- windows - GTK3 - 如何将窗口发送到后台
- python - 处理大量单词(>1 亿)以进行 LDA 分析时处理内存错误
- json - 在基于多个条件将json数据插入mongoDB时我们将如何忽略重复项
- jenkins - Jenkins JobDSL 不能用于配置 Maven 属性
- google-analytics - GA 360 导出到 Big Query
- s4sdk - 使用 Gradle 时,依赖项两次放入 Spring Boot (>2.x) jar