Python script to convert from UTF-8 to ASCII(将 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 脚本
基础教程推荐
猜你喜欢
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
