網站首頁 編程語言 正文
眾所周知,python之所以很方便在一定程度上是因為隨時都可能有人又創作了一個好用又方便的python非標準庫。
正好有一個小需求需要校驗一個python字符串中是否存在某種類型的字符,需求其實不難但是自己寫的話又要耗時費力,可能還存在BUG需要測試。
于是想找找看有沒有大佬已經實現這樣的python非標準庫,還真給找到了就是-txdpy,先安裝起來吧,確實比較方便給大佬遞茶!
pip install txdpy -i https://pypi.tuna.tsinghua.edu.cn/simple/
安裝完成之后將txdpy導入到我們的代碼塊中,對常用的函數進行測試執行是否能夠完成我們的常規邏輯處理。
# 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('是否純數字字符串:{0}'.format(tx.is_num(common_str)))
# It returns True if the input is a number, and False otherwise.
is_num()
結果執行之后以外情況發生了,依次報錯沒有導入下面的三個模塊。說明在我們的txdpy模塊中調用了下面的三個模塊,沒有關系,若是沒有安裝下面三個模塊的話安裝一下即可。
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'
將報錯的上面三個模塊使用pip的方式進行安裝,默認還是清華大學的鏡像站。如果沒有報錯證明已經安裝了,直接執行就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/
安裝成功了這個時候環境是沒啥問題,我們接著執行is_num函數,返回結果為False,說明不是純數字的字符串,結果正確。
2022-09-17 20:11:05.245 | INFO ? ? | __main__:is_contain_num:26 - 是否包含數字:False
接下來再執行幾個字符串是否為某種純字符的校驗,然后依次使用logger模塊打印出結果查看是否能夠完成準確的校驗。
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('是否數字、字母字符串:{0}'.format(tx.is_num_letter(common_str)))
is_num_letter()
2022-09-17 20:27:44.313 | INFO | __main__:is_num_letter:80 - 是否數字、字母字符串:True
除此之外,還有幾個比較使用的函數就是可以將字符串中的某種類型的字符串通過函數提取出來,它的底層是通過不同的正則表達式來實現的,不用我們再去考慮使用各式各樣的正則表達式來匹配數據了。
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']
以上是我們使用到的一些比較的常規的字符串處理的用法,還有更多的比較方便的函數的調用大家可以在使用中多看看,可以為我們的業務開發節省更多的時間
原文鏈接:https://www.cnblogs.com/lwsbc/p/16785832.html
相關推薦
- 2023-05-06 docker?search命令的具體使用_docker
- 2022-08-02 詳解Python?NumPy中矩陣和通用函數的使用_python
- 2022-08-26 教你用python從日期中獲取年、月、日和星期等30種信息_python
- 2022-05-08 Python?matplotlib實現散點圖的繪制_python
- 2022-12-09 C++筆記-設置cout輸出數據的寬度和填充方式_C 語言
- 2022-09-03 python?解決數據庫寫入時float自動變為整數的問題_python
- 2023-03-20 C#中程序自刪除實現方法_C#教程
- 2023-01-12 python數據擬合之scipy.optimize.curve_fit解讀_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支