Python 2 中字母在 Python 3 中的等价性是什么? [英] What is the equivalence in Python 3 of letters in Python 2?

查看:45
本文介绍了Python 2 中字母在 Python 3 中的等价性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 2 中你得到

<预><代码>>>>从字符串导入 *>>>字母'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

但是在 Python 3 中,你会得到

<预><代码>>>>从字符串导入 *>>>字母回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>NameError:未定义名称字母"

它没有定义,而digitswhitespace是.

Python 3 中字符串模块中 letters 的等价性是什么?

解决方案

在此处尝试使用:string.ascii_letters 而不是 letters.

更多信息:http://docs.python.org/release/3.1.3/library/string.html#string-constants


更新:

正如@wim 在 之前发表评论,这个建议在 Python 3 中使用 string.ascii_letters 不等同于 Python 2 中的 letters.就像 wim 指出的, string.ascii_letters 是不依赖于语言环境letters 取决于语言环境.

不过,我希望这个建议仍然有用,但希望包含来自 @wim 和文档的反馈.

In Python 2 you get

>>> from string import *
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

But in Python 3, you get

>>> from string import *
>>> letters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'letters' is not defined

It's not defined, whereas digits and whitespace are.

What is the equivalence of letters from the string module in Python 3?

解决方案

Try using: string.ascii_letters instead of just letters, here.

More information here: http://docs.python.org/release/3.1.3/library/string.html#string-constants


Update:

As @wim noted in the previously posted comment, this suggestion to use string.ascii_letters in Python 3 is not equivalent to the letters in Python 2. Like wim noted, string.ascii_letters is not locale-dependent while letters is locale-dependent.

I hope this suggestion can still be helpful, though, but wanted to include the feedback from @wim and the docs.

这篇关于Python 2 中字母在 Python 3 中的等价性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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