zoukankan      html  css  js  c++  java
  • parse库,更友好的格式化数据提取方案

    从一段指定的字符串中,取得期望的数据,正常人都会想到正则表达式吧?

    写过正则表达式的人都知道,正则表达式入门不难,写起来也容易。

    但是正则表达式几乎没有可读性可言,维护起来,真的会让人抓狂,别以为这段正则是你写的就可以驾驭它,过个一个月你可能就不认识它了。

    完全可以说,天下苦正则久矣。

    今天给你介绍一个好东西,可以让你摆脱正则的噩梦,那就是 Python 中一个非常冷门的库 -- parse 。

    1. 真实案例

    拿一个最近使用 parse 的真实案例来举例说明。

    下面是 ovs 一个条流表,现在我需要收集提取一个虚拟机(网口)里有多少流量、多少包流经了这条流表。也就是每个 in_port 对应的 n_bytes、n_packets 的值 。

    cookie=0x9816da8e872d717d, duration=298506.364s, table=0, n_packets=480, n_bytes=20160, priority=10,ip,in_port="tapbbdf080b-c2" actions=NORMAL
    

    如果是你,你会怎么做呢?

    先以逗号分隔开来,再以等号分隔取出值来?

    你不防可以尝试一下,写出来的代码应该和我想象的一样,没有一丝美感而言。

    我来给你展示一下,我是怎么做的?

    可以看到,我使用了一个叫做 parse 的第三方包,是需要自行安装的

    $ python -m pip install parse
    

    从上面这个案例中,你应该能感受到 parse 对于解析规范的字符串,是非常强大的。

    1. parse 的结果

    parse 的结果只有两种结果:

    1. 没有匹配上,parse 的值为None
        >>> parse("halo", "hello") is None
        True
        >>>
    
    1. 如果匹配上,parse 的值则 为 Result 实例
        >>> parse("hello", "hello world")
        >>> parse("hello", "hello")
        <Result () {}>
        >>> 
    

    如果你编写的解析规则,没有为字段定义字段名,也就是匿名字段, Result 将是一个 类似 list 的实例,演示如下:

    >>> profile = parse("I am {}, {} years old, {}", "I am Jack, 27 years old, male")
    >>> profile
    <Result ('Jack', '27', 'male') {}>
    >>> profile[0]
    'Jack'
    >>> profile[1]
    '27'
    >>> profile[2]
    'male'
    

    而如果你编写的解析规则,为字段定义了字段名, Result 将是一个 类似 字典 的实例,演示如下:

    >>> profile = parse("I am {name}, {age} years old, {gender}", "I am Jack, 27 years old, male")
    >>> profile
    <Result () {'gender': 'male', 'age': '27', 'name': 'Jack'}>
    >>> profile['name']
    'Jack'
    >>> profile['age']
    '27'
    >>> profile['gender']
    'male'
    
    1. 重复利用 pattern

    和使用 re 一样,parse 同样支持 pattern 复用。

    >>> from parse import compile
    >>> 
    >>> pattern = compile("I am {}, {} years old, {}")
    >>> pattern.parse("I am Jack, 27 years old, male")
    <Result ('Jack', '27', 'male') {}>
    >>> 
    >>> pattern.parse("I am Tom, 26 years old, male")
    <Result ('Tom', '26', 'male') {}>
    
    1. 类型转化

    从上面的例子中,你应该能注意到,parse 在获取年龄的时候,变成了一个"27" ,这是一个字符串,有没有一种办法,可以在提取的时候就按照我们的类型进行转换呢?

    你可以这样写。

    >>> from parse import parse
    >>> profile = parse("I am {name}, {age:d} years old, {gender}", "I am Jack, 27 years old, male")
    >>> profile
    <Result () {'gender': 'male', 'age': 27, 'name': 'Jack'}>
    >>> type(profile["age"])
    <type 'int'>
    

    除了将其转为 整型,还有其他格式吗?

    内置的格式还有很多,比如

    匹配时间

    >>> parse('Meet at {:tg}', 'Meet at 1/2/2011 11:00 PM')
    <Result (datetime.datetime(2011, 2, 1, 23, 0),) {}>
    

    更多类型请参考官方文档:

    Type Characters Matched Output
    l Letters (ASCII) str
    w Letters, numbers and underscore str
    W Not letters, numbers and underscore str
    s Whitespace str
    S Non-whitespace str
    d Digits (effectively integer numbers) int
    D Non-digit str
    n Numbers with thousands separators (, or .) int
    % Percentage (converted to value/100.0) float
    f Fixed-point numbers float
    F Decimal numbers Decimal
    e Floating-point numbers with exponent e.g. 1.1e-10, NAN (all case insensitive) float
    g General number format (either d, f or e) float
    b Binary numbers int
    o Octal numbers int
    x Hexadecimal numbers (lower and upper case) int
    ti ISO 8601 format date/time e.g. 1972-01-20T10:21:36Z (“T” and “Z” optional) datetime
    te RFC2822 e-mail format date/time e.g. Mon, 20 Jan 1972 10:21:36 +1000 datetime
    tg Global (day/month) format date/time e.g. 20/1/1972 10:21:36 AM +1:00 datetime
    ta US (month/day) format date/time e.g. 1/20/1972 10:21:36 PM +10:30 datetime
    tc ctime() format date/time e.g. Sun Sep 16 01:03:52 1973 datetime
    th HTTP log format date/time e.g. 21/Nov/2011:00:07:11 +0000 datetime
    ts Linux system log format date/time e.g. Nov 9 03:37:44 datetime
    tt Time e.g. 10:21:36 PM -5:30 time
    1. 提取时去除空格

    去除两边空格

    >>> parse('hello {} , hello python', 'hello     world    , hello python')
    <Result ('    world   ',) {}>
    >>> 
    >>> 
    >>> parse('hello {:^} , hello python', 'hello     world    , hello python')
    <Result ('world',) {}>
    

    去除左边空格

    >>> parse('hello {:>} , hello python', 'hello     world    , hello python')
    <Result ('world   ',) {}>
    

    去除右边空格

    >>> parse('hello {:<} , hello python', 'hello     world    , hello python')
    <Result ('    world',) {}>
    
    1. 大小写敏感开关

    Parse 默认是大小写不敏感的,你写 hello 和 HELLO 是一样的。

    如果你需要区分大小写,那可以加个参数,演示如下:

    >>> parse('SPAM', 'spam')
    <Result () {}>
    >>> parse('SPAM', 'spam') is None
    False
    >>> parse('SPAM', 'spam', case_sensitive=True) is None
    True
    
    1. 匹配字符数

    精确匹配:指定最大字符数

    >>> parse('{:.2}{:.2}', 'hello')  # 字符数不符
    >>> 
    >>> parse('{:.2}{:.2}', 'hell')   # 字符数相符
    <Result ('he', 'll') {}>
    

    模糊匹配:指定最小字符数

    >>> parse('{:.2}{:2}', 'hello') 
    <Result ('h', 'ello') {}>
    >>> 
    >>> parse('{:2}{:2}', 'hello') 
    <Result ('he', 'llo') {}>
    

    若要在精准/模糊匹配的模式下,再进行格式转换,可以这样写

    >>> parse('{:2}{:2}', '1024') 
    <Result ('10', '24') {}>
    >>> 
    >>> 
    >>> parse('{:2d}{:2d}', '1024') 
    <Result (10, 24) {}>
    
    1. 三个重要属性

    Parse 里有三个非常重要的属性

    • fixed:利用位置提取的匿名字段的元组
    • named:存放有命名的字段的字典
    • spans:存放匹配到字段的位置

    下面这段代码,带你了解他们之间有什么不同

    >>> profile = parse("I am {name}, {age:d} years old, {}", "I am Jack, 27 years old, male")
    >>> profile.fixed
    ('male',)
    >>> profile.named
    {'age': 27, 'name': 'Jack'}
    >>> profile.spans
    {0: (25, 29), 'age': (11, 13), 'name': (5, 9)}
    >>> 
    
    1. 自定义类型的转换

    匹配到的字符串,会做为参数传入对应的函数

    比如我们之前讲过的,将字符串转整型

    >>> parse("I am {:d}", "I am 27")
    <Result (27,) {}>
    >>> type(_[0])
    <type 'int'>
    >>> 
    

    其等价于

    >>> def myint(string):
    ...     return int(string)
    ... 
    >>> 
    >>> 
    >>> parse("I am {:myint}", "I am 27", dict(myint=myint))
    <Result (27,) {}>
    >>> type(_[0])
    <type 'int'>
    >>>
    

    利用它,我们可以定制很多的功能,比如我想把匹配的字符串弄成全大写

    >>> def shouty(string):
    ...    return string.upper()
    ...
    >>> parse('{:shouty} world', 'hello world', dict(shouty=shouty))
    <Result ('HELLO',) {}>
    >>>
    

    10案例+1
    在工作中,我们开发的系统会涉及到大量的日志。同时,我们还有另一套系统会对日志的内容进行监控,从而判断系统是否正常运作。

    以 Nginx 的日志为例,这是一条访问日志:

    162.158.167.131 - - [11/Aug/2020:06:47:30 +0800] "GET /tags/Tenacity HTTP/1.1" 301 194 "-" "Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)"
    

    这条日志包含了很多信息,包括:访问者的 IP 地址:162.158.167.131,访问发起的时间:11/Aug/2020:06:47:30 +0800,具体访问的路径:/tags/Tenacity,访问者的 User-Agent 等等。

    一般情况下,我们可能需要编写正则表达式来提取这些信息,大家可以现在试一试,针对上面的日志,如果让你来写正则表达式,你会怎么写。

    现在,我们有更好的选择,那就是 parse这个第三方库。用它能够更加友好又方便地通过简单正则来提取复杂的内容。

    我们可以使用pip安装它:

    python3 -m pip install parse
    

    安装完成以后,我们用一段简单的代码来进行测试:

    >>> import parse
    >>> log = '162.158.167.131 - - [11/Aug/2020:06:47:30 +0800] "GET /tags/Tenacity HTTP/1.1" 301 194 "-" "Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)"'
    >>> pattern = '{ip} - - [{dt:th}] "{method} {path} HTTP/1.1" {code:d} {length:d} "-" "{ua}"'
    >>> result = parse.search(pattern, log)
    >>> result['ip']'162.158.167.131'
    >>> result['ua']'Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)'
    >>> print(result.named)
    {'ip': '162.158.167.131', 'dt': datetime.datetime(2020, 8, 11, 6, 47, 30, tzinfo=<FixedTzOffset +0800 8:00:00>), 'method': 'GET', 'path': '/tags/Tenacity', 'code': 301, 'length': 194, 'ua': 'Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)'}
    

    运行效果如下图所示:

    非常轻松地就把需要的字段全部以字典的形式提取了出来。并且日期、数字可以直接提取成对应的形式,免去了事后转换的麻烦。

    只要我们自己系统的日志,按照统一的规范来写,那么也可以非常轻易地提取出来。例如我在爬虫多次爬取失败时,写出如下一条日志:

    2020-08-11 13:21:41 [scrapy.extensions.logstats] INFO: [多次失败] https://xxx.com/aa/bb
    
    

    那么我可以把提取的规则写为:

    pattern = '[多次失败] {url}
    '
    

    运行效果如下图所示:

    关于 parse 的更多用法,请看它的 Readme[1]。

    参考资料

    [1]Readme: https://github.com/r1chardj0n3s/parse

    11 总结一下

    parse 库在字符串解析处理场景中提供的便利,肉眼可见,上手简单。

    在一些简单的场景中,使用 parse 可比使用 re 去写正则开发效率不知道高几个 level,用它写出来的代码富有美感,可读性高,后期维护起代码来一点压力也没有,推荐你使用。

  • 相关阅读:
    HTML <form> 标签的 method 属性(20161028)
    PHP数据访问增删查(20161028)
    PHP数据访问基础知识(20161028)
    java代理机制
    java 模拟实现消费者和生产者问题
    Single Number II
    从1到1000中随机取出900个不重复的随机数
    取苹果方式总数
    Net-SNMP(V3协议)安装配置笔记(CentOS 5.2)(转)
    单例模式
  • 原文地址:https://www.cnblogs.com/lianhaifeng/p/13843648.html
Copyright © 2011-2022 走看看