绿色应用市场
当前位置:首页 > 学习教程 > 正文

Python实现对字符串中字符提取校验

发布时间:2022-10-13 09:17:38来源:周小白软件园编辑:本站整理

众所周知,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']

以上是我们使用到的一些比较的常规的字符串处理的用法,还有更多的比较方便的函数的调用大家可以在使用中多看看,可以为我们的业务开发节省更多的时间

到此这篇关于Python实现对字符串中字符提取校验的文章就介绍到这了,更多相关Python字符提取校验内容请搜索云初冀北以前的文章或继续浏览下面的相关文章希望大家以后多多支持云初冀北!

相关推荐