python-2.7 - Python 2.7 import unicode_literals from __future__ 在使用 umauts 读取文件时给出 UnicodeDecodeError
问题描述
我有一个 Python 脚本,它在输入文件“myfile.in”中读取和写入带有德语变音符号 (äöü) 的文件。我使用了 Python 2.7 版。这是我的脚本的简化版本:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
if __name__=='__main__':
with open("myfile.in", "r") as f:
lines = f.readlines()
txt = ""
for line in lines:
txt = txt + line
with open("myfile.out", "w") as f:
f.write(txt)
这工作正常。
现在我从客户那里得到了使用 Future 语句定义的要求,我在我的 Python 脚本中添加了以下行:
from __future__ import unicode_literals
现在我收到以下错误消息:
Traceback (most recent call last):
File "myscript.py", line 9, in <module>
txt = txt + line
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 23: ordinal not in range(128)
我该如何解决这个问题。谢谢你的提示托马斯
解决方案
推荐阅读
- node.js - 如何检查更新的文档是否真的被修改了?
- c# - 每个类获得一个 Roslyn 语法接收器
- python - 如何记录两个断点之间的调用堆栈?
- javascript - onChange 中 e(事件接口)的第二个参数 = (type) => (e) => 这是如何从 OnChange 传递的(默认情况下)
- c++ - 代码在第 51 个输入时失败,测试用例 2。可能是什么问题?
- angular - 摆脱 Angular 中的循环依赖
- python - python中`__slots__`的静态类型
- ios - 在 UITableView 的不同部分显示不同的行
- java - 在子存储库 findAll() 方法中返回父类型列表导致编译错误
- node.js - 如何在 Swift 中使用 body 发布/获取