python - python - 将字符串转换为 unicode 字符串
问题描述
我使用库 unidecode 将重音字符串转换为 ascii 表示的搅拌。
>>> accented_string = u'Málaga'
# accented_string is of type 'unicode'
>>> import unidecode
>>> unidecode.unidecode(accented_string)
>>> Malaga
但问题是我正在从文件中读取字符串,如何将其发送到“unidecode”库。
for name in strings:
print unidecode.unidecode(u+name) #?????
我摸不着头脑?如果我对其进行编码,那只会给我错误的编码。
解决方案
我们仍然不知道您的 pandas 列的类型,所以这里有两个 Python 2 版本:
如果
strings
已经是一个 Unicode 字符串序列 (type(name)
isunicode
):for name in strings: print unidecode.unidecode(name)
如果 的元素
strings
是常规 Python 2str
(type(name)
isstr
):for name in strings: print unidecode.unidecode(name.decode("utf-8"))
如果您的字符串以 UTF-8 编码存储,这将起作用。否则,您必须提供适当的编码,例如"latin-1"
等。
在 Python 3 中,第一个版本应该可以工作;在您到达这一点之前,您必须解决您的编码问题,即当您第一次从磁盘读取数据时。
推荐阅读
- sql-server - 我正在尝试将我的插入查询更改为批量插入,我该怎么做?
- powershell - Teams PowerShell:访问令牌验证失败
- redis - 处理 Azure Redis 缓存异常
- python - 使用 numpy 交换数组中一定百分比的元素
- java - 如何使用转义字符发送到 RabbitMQ
- php - 如何在选择元素内将单击的选项显示为活动的?
- angular - Nativescript nfc 插件似乎无法正常工作
- go - 如何在GO中通过firestore设置云功能触发器
- java - 如何使用 FileReader 确保在 Java 中找到文件?
- asp.net-core - TelemetryConfiguration 中的重复 ITelemetryInitializer