python-3.x - Python 产生 kysl� 而不是 kyslá
问题描述
解决方案
首先,您不需要对字符串进行任何编码和解码。
切勿在未明确指定文件编码的情况下打开文本文件。这是一个简单而普遍的规则。如果您不知道正在读取(或写入)的文件的编码,并让 Python 使用它感觉使用的任何默认值,那么所有的赌注都将失败。
HTML 文件往往采用 UTF-8 编码。另一个可能的候选者是 Windows-1252 ( cp1252
)。但实际上这取决于文件是如何创建的,因此您必须检查。
这应该非常接近您的想法。此代码的任何部分都不涉及对任何字符串进行编码 -open()
为您执行此操作。
with open("org22.htm", "r", encoding="utf8") as infile:
html = infile.read()
start = "I = new Array();"
end = "State = new Array();"
pos1 = html.find(start) + len(start)
pos2 = html.rfind(end)
lines = html[pos1:pos2].splitlines()
with open("dump.txt", "w", encoding="utf8") as outfile:
for i, line in enumerate(lines):
if f"I[{i}][1][0][0] =" in line:
data = line.split("'")[-2]
outfile.write(data + "\n")
推荐阅读
- python-packaging - Python 诗歌,安装可选依赖项
- python - 使用预测数据对 MultiIndex DataFrame 进行 LSTM/RNN 预处理
- javascript - 如何从末尾开始查找数组中的元素
- node.js - 多选问答的 Mongo 设计模式
- android - 如何设置接收器以在 SMS 应用程序中获取“已发送”和“已交付”报告(应用程序设置为默认应用程序)
- c# - 仅通过 XAML 将复选框绑定到元素可见性
- python - view.setColumnHidden (0, True) 不工作
- javascript - 我将如何在我的控制器中以 express 模拟 axios 调用?
- konvajs - 是否可以在 konva.js 中为 rect 元素设置模糊?
- javascript - JS 新手,通过计数类进行分页