日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

以后字符串中的字符提取校驗就用這個了,效果不錯!

 Python集中營 2022-10-12 發(fā)布于甘肅

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

立即前往微信小程序【知識星球】提問>>>

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

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

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

安裝完成之后將txdpy導(dǎo)入到我們的代碼塊中,對常用的函數(shù)進行測試執(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ā)生了,依次報錯沒有導(dǎo)入下面的三個模塊。說明在我們的txdpy模塊中調(diào)用了下面的三個模塊,沒有關(guān)系,若是沒有安裝下面三個模塊的話安裝一下即可。

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

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

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

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

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

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

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

安裝成功了這個時候環(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í)行幾個字符串是否為某種純字符的校驗,然后依次使用logger模塊打印出結(jié)果查看是否能夠完成準(zhǔ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

除此之外,還有幾個比較使用的函數(shù)就是可以將字符串中的某種類型的字符串通過函數(shù)提取出來,它的底層是通過不同的正則表達式來實現(xiàn)的,不用我們再去考慮使用各式各樣的正則表達式來匹配數(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é)省更多的時間,感謝粉絲朋友們一直以來的支持!

tabulate結(jié)合loguru打印出美觀又方便查找的日志記錄!

我使用pangu模塊做了一個文本格式化小工具!

ping命令的多種玩法,以前竟然只用它來測試網(wǎng)速!

為方便數(shù)據(jù)分析,實現(xiàn)Python對象與DataFrame數(shù)據(jù)的相互轉(zhuǎn)換!

    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多