关于python:以后字符串中的字符提取校验就用这个了效果不错

6次阅读

共计 2854 个字符,预计需要花费 8 分钟才能阅读完成。

家喻户晓,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 = '[email protected] 集中营.'

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 = '[email protected] 集中营.'

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']

以上是咱们应用到的一些比拟的惯例的字符串解决的用法,还有更多的比拟不便的函数的调用大家能够在应用中多看看,能够为咱们的业务开发节俭更多的工夫,感激粉丝敌人们始终以来的反对!

tabulate 联合 loguru 打印出好看又不便查找的日志记录!

我应用 pangu 模块做了一个文本格式化小工具!

为不便数据分析,实现 Python 对象与 DataFrame 数据的互相转换!

正文完
 0