python - 读取存储为字节的文本文件
问题描述
我有一个文本文件,其中项目存储为字节,即b" Charg\xc3\xa9e de mission communication chez BRL Groupe"
. 如何将其读入 python 以便将其解码为 utf-8?
将其作为 'rb' 读入 python 将其转换为b'b" Charg\\xc3\\xa9e de mission communication chez BRL Groupe"'
.
with open('bytes.txt', 'rb') as f:
for line in f:
print(line)
我想得到它,以便它打印解码的字符串,Chargée de mission communication chez BRL Groupe
解决方案
而不是解码,它将字符串转换为字节,你需要使用一些东西来评估字符串是什么。那就是 ast.literal_eval()
print(ast.literal_eval(line))
感谢您的投票!这是答案!
推荐阅读
- r - 如何对此数据进行 Tukey 的 HSD 测试?
- c# - 集成测试 - 如何使用 Microsoft.Owin.Testing.TestServer 命中端点?
- python - 如何在 Mac 上安装 pip?
- angular - NgFor 嵌套组件未在 @Input 更改时更新
- c - 香草 C 替代英特尔内在函数?
- swift - 具有圆形边缘的自定义表格视图单元格
- c++ - 将值复制到多维数组时出现问题
- python - 在编码基于列表的回文检查器时遇到问题
- html - HTML“for”标签不适用于
但适用于
- reporting-services - 通过参数选择将来自两个不同数据库服务器的结果集显示到 SSRS 中的一个报告中