unicodedata.normalize在python中做什么? [英] What does unicodedata.normalize do in python?

查看:471
本文介绍了unicodedata.normalize在python中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

import unicodedata
my_var = "this is a string"
my_var2 = " Esta es una oración que está en español "
my_var3 = unicodedata.normalize('NFKD', my_var2).encode('ascii', 'ignore')
output = my_var + my_var3
print(output)

Python结束并显示以下错误.

And python finishes with the following error.

**File "C:/path/to/my/file/testing_file.py", line 5, in <module>
    output = my_var + my_var3
TypeError: Can't convert 'bytes' object to str implicitly
Process finished with exit code 1**

我想知道这段代码是做什么的?这个逻辑正在另一个开发人员的另一个项目上实现,我完全不理解.

I would like to know what does this code do? This logic is being implemented on another project from another developer and I can't understand it at all.

我该如何解决这个问题?我需要一个字符串,以后将对其进行处理.

How can I solve this problem? I need a string which I will manipulate after.

推荐答案

您需要指定编码类型.

然后您需要使用unicode而不是字符串作为normalize()的参数

Then you need to use unicode instead of string as arguments of normalize()

# -*- coding: utf-8 -*-

import unicodedata
my_var = u"this is a string"
my_var2 = u" Esta es una oración que está en español "
my_var3 = unicodedata.normalize(u'NFKD', my_var2).encode('ascii', 'ignore').decode('utf8')
output = my_var + my_var3
print(output)

这篇关于unicodedata.normalize在python中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆