V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
songdg
V2EX  ›  Python

判断是否字母的问题

  •  
  •   songdg · 2020-08-30 18:27:22 +08:00 · 2135 次点击
    这是一个创建于 1327 天前的主题,其中的信息可能已经有所发展或是发生改变。
    网上找的一个函数,为什么不能判断大写字母
    def is_alphabet(uchar):
    """判断一个 unicode 是否是英文字母"""
    if (uchar >= u'\u0041' and uchar<=u'\u005A') or (uchar >= u'\u0061' and uchar<=u'\u007A'):
    return True
    else:
    return False
    9 条回复    2020-09-12 07:03:07 +08:00
    imn1
        1
    imn1  
       2020-08-30 18:36:00 +08:00
    什么意思?
    is_alphabet('A') 返回 False 么?
    skinny
        2
    skinny  
       2020-08-30 19:31:38 +08:00
    Python 没有 char 类型
    skinny
        3
    skinny  
       2020-08-30 19:35:25 +08:00
    你一定要按这个函数的样子写,你用 chr 函数把单个字符转换成 int
    baobao1270
        4
    baobao1270  
       2020-08-30 19:53:38 +08:00
    (uchar >= u'\u0041' and uchar<=u'\u005A') 是 ASCII 大写字母 A-Z
    (uchar >= u'\u0061' and uchar<=u'\u007A') 是 ASCII 小写字母 a-z
    注释都说了,这个智能判断是否为字母,而不能判断是大写还是小写……
    你这个写法也不够 Pythonic,可以写成
    (u'\u0041' <= uchar <=u'\u005A') or (u'\u0061' <= uchar <=u'\u007A')

    @skinny
    Python 是可以直接比较字符串的 ASCII 值的

    可以尝试以下代码:
    from enum import Enum

    class InAlphabetResult(Enum):
    UpperCase = 1
    LowerCase = 2
    NotInAlphabet = 3

    def in_alphabet(char):
    if "a" <= char <= "z":
    return InAlphabetResult.LowerCase
    if "A" <= char <= "Z":
    return InAlphabetResult.UpperCase
    return InAlphabetResult.NotInAlphabet
    baobao1270
        5
    baobao1270  
       2020-08-30 19:55:35 +08:00   ❤️ 1
    缩进没了,我贴个链接吧
    https://pastebin.ubuntu.com/p/MGZqgWGCSr/
    songdg
        6
    songdg  
    OP
       2020-08-31 01:24:17 +08:00
    @baobao1270 谢谢帮忙。
    laike9m
        7
    laike9m  
       2020-08-31 10:24:21 +08:00   ❤️ 1
    你这完全没必要啊,直接标准库 isupper 函数就完了的事

    https://docs.python.org/3.8/library/stdtypes.html#str.isupper
    songdg
        8
    songdg  
    OP
       2020-09-02 09:24:45 +08:00
    @laike9m 对,用标准库更方便。
    biglazycat
        9
    biglazycat  
       2020-09-12 07:03:07 +08:00
    def in_alphabet(char):
    char = str(char)
    if char.islower():
    return 'LowerCase'
    elif char.isupper():
    return 'UpperCase'
    else:
    return 'NotInAlphabet'

    print(in_alphabet('a'))
    print(in_alphabet('A'))
    print(in_alphabet(1))

    写的很粗糙,目前的理解就写样了。请多多指教。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3282 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:21 · PVG 20:21 · LAX 05:21 · JFK 08:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.