Python script to convert from UTF-8 to ASCII(将 UTF-8 转换为 ASCII 的 Python 脚本)
本文介绍了将 UTF-8 转换为 ASCII 的 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用 python 编写一个脚本来将 utf-8 文件转换为 ASCII 文件:
I'm trying to write a script in python to convert utf-8 files into ASCII files:
#!/usr/bin/env python
# *-* coding: iso-8859-1 *-*
import sys
import os
filePath = "test.lrc"
fichier = open(filePath, "rb")
contentOfFile = fichier.read()
fichier.close()
fichierTemp = open("tempASCII", "w")
fichierTemp.write(contentOfFile.encode("ASCII", 'ignore'))
fichierTemp.close()
运行此脚本时出现以下错误:
When I run this script I have the following error :
UnicodeDecodeError: 'ascii' 编解码器无法解码位置 13 中的字节 0xef:序号不在范围内(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 13: ordinal not in range(128)
我认为可以通过 encode 方法中的 ignore 参数忽略错误.但好像不是.
I thought that can ignore error with the ignore parameter in the encode method. But it seems not.
我愿意接受其他转换方式.
I'm open to other ways to convert.
推荐答案
data="UTF-8 DATA"
udata=data.decode("utf-8")
asciidata=udata.encode("ascii","ignore")
这篇关于将 UTF-8 转换为 ASCII 的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:将 UTF-8 转换为 ASCII 的 Python 脚本


基础教程推荐
猜你喜欢
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01