Difference between encoding utf-8 and utf8 in Python 3.5

女生的网名这么多〃 提交于 2020-12-08 05:22:26

问题


What is the difference between encoding utf-8 and utf8 (if there is any)?

Given the following example:

u = u'€'
print('utf-8', u.encode('utf-8'))
print('utf8 ', u.encode('utf8'))

It produces the following output:

utf-8 b'\xe2\x82\xac'
utf8  b'\xe2\x82\xac'

回答1:


There's no difference. See the table of standard encodings. Specifically for 'utf_8', the following are all valid aliases:

'U8', 'UTF', 'utf8'

Also note the statement in the first paragraph:

Notice that spelling alternatives that only differ in case or use a hyphen instead of an underscore are also valid aliases; therefore, e.g. 'utf-8' is a valid alias for the 'utf_8' codec




回答2:


You can also check the aliases of a specific encoding using encodings module, this way, which will give you a Key matching aliases as values:

>>> from encodings.aliases import aliases
>>> 
>>> for k,v in aliases.items():
    if 'utf_8' in v:
        print('Encoding name:{:>10} -- Module Name: {:}'.format(k,v))


Encoding name:       utf -- Module Name: utf_8
Encoding name:        u8 -- Module Name: utf_8
Encoding name: utf8_ucs4 -- Module Name: utf_8
Encoding name: utf8_ucs2 -- Module Name: utf_8
Encoding name:      utf8 -- Module Name: utf_8

And as pointed by the mgilson's answer:

Notice that spelling alternatives that only differ in case or use a hyphen instead of an underscore are also valid aliases; therefore, e.g. 'utf-8' is a valid alias for the 'utf_8' codec.



来源:https://stackoverflow.com/questions/35383506/difference-between-encoding-utf-8-and-utf8-in-python-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!