欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)對字符串中字符提取校驗(yàn)

 更新時(shí)間:2022年10月13日 08:18:10   作者:Python 集中營  
眾所周知,python之所以很方便在一定程度上是因?yàn)殡S時(shí)都可能有人又創(chuàng)作了一個(gè)好用又方便的python非標(biāo)準(zhǔn)庫。本文就來用Python實(shí)現(xiàn)對字符串中字符進(jìn)行提取校驗(yàn),需要的可以參考一下

眾所周知,python之所以很方便在一定程度上是因?yàn)殡S時(shí)都可能有人又創(chuàng)作了一個(gè)好用又方便的python非標(biāo)準(zhǔn)庫。

正好有一個(gè)小需求需要校驗(yàn)一個(gè)python字符串中是否存在某種類型的字符,需求其實(shí)不難但是自己寫的話又要耗時(shí)費(fèi)力,可能還存在BUG需要測試。

于是想找找看有沒有大佬已經(jīng)實(shí)現(xiàn)這樣的python非標(biāo)準(zhǔn)庫,還真給找到了就是-txdpy,先安裝起來吧,確實(shí)比較方便給大佬遞茶!

pip install txdpy -i https://pypi.tuna.tsinghua.edu.cn/simple/

安裝完成之后將txdpy導(dǎo)入到我們的代碼塊中,對常用的函數(shù)進(jìn)行測試執(zhí)行是否能夠完成我們的常規(guī)邏輯處理。

# Importing the txdpy module and renaming it to tx.
import txdpy as tx

from loguru import logger

# A string that is used to test the functions in the txdpy module.
common_str = '123er45io9@Python 集中營.'

def is_num():
    """
    It returns True if the input is a number, and False otherwise
    """

    # A logging statement.
    logger.info('是否純數(shù)字字符串:{0}'.format(tx.is_num(common_str)))

# It returns True if the input is a number, and False otherwise.
is_num()

結(jié)果執(zhí)行之后以外情況發(fā)生了,依次報(bào)錯沒有導(dǎo)入下面的三個(gè)模塊。說明在我們的txdpy模塊中調(diào)用了下面的三個(gè)模塊,沒有關(guān)系,若是沒有安裝下面三個(gè)模塊的話安裝一下即可。

File "C:\software\python\lib\site-packages\txdpy\requests_operation.py", line 1, in <module>
    from lxml import etree
ModuleNotFoundError: No module named 'lxml'

File "C:\software\python\lib\site-packages\txdpy\PyReBf.py", line 1, in <module>
    import mmh3
ModuleNotFoundError: No module named 'mmh3'

  File "C:\software\python\lib\site-packages\txdpy\PyReBf.py", line 2, in <module>
    import redis
ModuleNotFoundError: No module named 'redis'

將報(bào)錯的上面三個(gè)模塊使用pip的方式進(jìn)行安裝,默認(rèn)還是清華大學(xué)的鏡像站。如果沒有報(bào)錯證明已經(jīng)安裝了,直接執(zhí)行就OK了。

pip install lxml -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip install mmh3 -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip install redis -i https://pypi.tuna.tsinghua.edu.cn/simple/

安裝成功了這個(gè)時(shí)候環(huán)境是沒啥問題,我們接著執(zhí)行is_num函數(shù),返回結(jié)果為False,說明不是純數(shù)字的字符串,結(jié)果正確。

2022-09-17 20:11:05.245 | INFO     | __main__:is_contain_num:26 - 是否包含數(shù)字:False

接下來再執(zhí)行幾個(gè)字符串是否為某種純字符的校驗(yàn),然后依次使用logger模塊打印出結(jié)果查看是否能夠完成準(zhǔn)確的校驗(yàn)。

def is_letter():
    """
    It checks if the input is a letter.
    """

    # A logging statement.
    logger.info('是否純字母字符串:{0}'.format(tx.is_letter(common_str)))

# It checks if the input is a letter.
is_letter()

2022-09-17 20:24:36.232 | INFO     | __main__:is_letter:66 - 是否純字母字符串:False

def is_num_letter():
    """
        It checks if the input is a letter or num.
    """

    common_str = '123com'

    logger.info('是否數(shù)字、字母字符串:{0}'.format(tx.is_num_letter(common_str)))

is_num_letter()

2022-09-17 20:27:44.313 | INFO     | __main__:is_num_letter:80 - 是否數(shù)字、字母字符串:True

除此之外,還有幾個(gè)比較使用的函數(shù)就是可以將字符串中的某種類型的字符串通過函數(shù)提取出來,它的底層是通過不同的正則表達(dá)式來實(shí)現(xiàn)的,不用我們再去考慮使用各式各樣的正則表達(dá)式來匹配數(shù)據(jù)了。

common_str = '123er45io9@Python 集中營.'

def get_chinese():
    """
    It returns the string "Chinese"
    """

    # A logging statement.
    logger.info('提取到字符串中的中文是:{0}'.format(tx.get_chinese(common_str)))

# It returns the string "Chinese"
get_chinese()

2022-09-17 20:39:40.356 | INFO     | __main__:get_chinese:102 - 提取到字符串中的中文是:['集中營']

def get_num():
    """
    It returns the number of times the function has been called.
    """

    # A variable that is used to store the number of times the function has been called.
    logger.info('提取到字符串中的中文是:{0}'.format(tx.get_num(common_str)))

get_num()

2022-09-17 20:41:27.998 | INFO     | __main__:get_num:115 - 提取到字符串中的中文是:['123', '45', '9']

以上是我們使用到的一些比較的常規(guī)的字符串處理的用法,還有更多的比較方便的函數(shù)的調(diào)用大家可以在使用中多看看,可以為我們的業(yè)務(wù)開發(fā)節(jié)省更多的時(shí)間

到此這篇關(guān)于Python實(shí)現(xiàn)對字符串中字符提取校驗(yàn)的文章就介紹到這了,更多相關(guān)Python字符提取校驗(yàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論