python Windows-1254转utf-8
时间: 2023-08-19 16:55:49 浏览: 796
要将 Windows-1254 编码的文本转换为 UTF-8 编码,你可以使用 Python 的 `codecs` 模块来实现。下面是一个示例代码:
```python
import codecs
def convert_encoding(input_file, output_file):
with codecs.open(input_file, 'r', 'Windows-1254') as file:
content = file.read()
with codecs.open(output_file, 'w', 'utf-8') as file:
file.write(content)
# 使用示例
input_file = 'input.txt' # 输入文件路径
output_file = 'output.txt' # 输出文件路径
convert_encoding(input_file, output_file)
```
在上面的示例中,我们首先使用 `codecs.open` 函数打开输入文件,并指定使用 Windows-1254 编码进行读取。然后,我们读取文件内容并保存到 `content` 变量中。
接下来,我们使用 `codecs.open` 函数打开输出文件,并指定使用 UTF-8 编码进行写入。然后,我们将 `content` 变量中的内容写入到输出文件中,从而实现编码转换。
你只需要将示例代码中的 `input_file` 和 `output_file` 变量替换为你实际使用的文件路径即可。请确保输入文件存在,并且具有 Windows-1254 编码的文本内容。转换后的 UTF-8 编码文本将保存到输出文件中。
阅读全文
相关推荐

















