在Python中,`%`格式操作符与`str.format()`在unicode和utf-8编码上有区别吗?

7 投票
1 回答
693 浏览
提问于 2025-04-17 08:50

假设有以下内容:

n = u"Tübingen"
repr(n) # `T\xfcbingen` # Unicode
i = 1 # integer

下面的第一个文件出现了错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 82: ordinal not in range(128)

当我执行 n.encode('utf8') 时,它可以正常工作。

第二个文件在两种情况下都能完美运行。

# Python File 1
#
#!/usr/bin/env python -B
# encoding: utf-8

print '{id}, {name}'.format(id=i, name=n)

# Python File 2
#
#!/usr/bin/env python -B
# encoding: utf-8

print '%i, %s'% (i, n)

因为在文档中建议使用 format() 而不是 % 格式操作符,所以我不明白为什么 format() 看起来更“受限制”。难道 format() 只适用于 utf8 字符串吗?

1 个回答

10

你在使用 string.format,但其实你手里的是一个 unicode 对象,而不是字符串。

print u'{id}, {name}'.format(id=i, name=n)

这样做是可以的,因为它使用的是 unicode.format

撰写回答