python - python读取csv受utf-8编码干扰
问题描述
我试图在 python 中使用 pandas 读取 csv 文件,后来我试图读取一个文本文件并用 csv 中的值替换内容我得到以下错误:
lines = [line.decode('utf-8').strip() for line in lines]
File "/usr/local/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 140: invalid start byte
解决方案
尝试在下面使用。它以一种非常简单的方式对我有用。
str= str.decode('unicode_escape').encode('utf-8')
如果您正在阅读文本文件,那么在这里您需要遍历列表的每个元素,您可以尝试以下操作
lines= open("file.text", "r")
lines = fileread.readlines()
lines = [line.decode('unicode_escape').encode('utf-8') for line in lines]
希望这可以帮助...
推荐阅读
- database - Mongodb TTL索引不会使集合中的文档过期
- excel - Excel Activex 文本框在两台计算机上的操作方式不同
- php - 使用 PHP(无 JavaScript)在元素单击时切换显示
- python - TypeError:无法转换值
我正在尝试
tf.GradientTape
在我的模型中应用。在此之前,我正在尝试一个玩具示例。import numpy as np import tensorflow as tf X =
- python - Python HTML 解析器(未命名级别)
- matplotlib - plot_surface 减少点的密度
- java - Java中给一个变量赋值2个值是什么意思
- javascript - 在 HTML 字符串中调用函数时,TypeScript 'this' 未定义
- html - 无法让 div 与 css 正确对齐
- python - 如何在pyqt5中调整垂直框布局中添加的小部件的几何形状