python - 如何从字符串中删除某些 utf-8 字符?
问题描述
解决方案
The line
variable in your code is a unicode
object. When you call line.replace
Python expects the first argument to also be a unicode
object. If you provide a str
object instead, Python will try to automatically decode it into a unicode
string using the system default encoding (which you can check via sys.getdefaultencoding()
).
Apparently, the system encoding is ascii
in your case. The byte string '„'
cannot be decoded using the ascii
codec, because '„'
is not an ACII symbol, which causes the Exception that you see.
You could fix the problem by changing the default system encoding to the same one you used to provide the '„'
string (CP1252, I guess), however such a fix is only interesting from the academic point of view, as it just sweeps the issue under the carpet.
A proper, safe and easy fix to your problem would be to simply provide a unicode
object to the replace
method in the first place. This would be as simple as replacing '„'
with u'„'
in your code.
推荐阅读
- javascript - React Material UI 模式未打开
- python - 在 Python 中拆分列后分配多个列名(400 列)的快速方法
- javascript - 从 C# 单元测试调用 Javascript 函数
- python - 使用单词列表从数据框中定位行
- unit-testing - 如何模拟被测同一类中的方法?
- utf-8 - 为什么即使设置了语言环境也会出现错误?
- apache - htaccess 仅限制对 index.php 的访问
- tasmota - 无论如何让tasmota模仿按钮点击?
- c# - 在结构中编组 C++ win32 类型以在 C# 中的函数中使用
- python - PyTube:PrivateVideo 异常不起作用