zoukankan      html  css  js  c++  java
  • 第4章:文本处理

    1.字符串常量

    1).字符串是不可变的有序集合

         Python不区分字符和字符串

        所有的字符都是字符串

        字符串是不可变的

        字符串是字符的有序集合

    2).字符串函数

        通用操作

        与大小写相关的方法

        判断类方法

        字符串方法startswith和endswith

        查找类函数

        字符串操作方法

    3).使用Python分析Apache的访问日志

    from __future__ import print_function
    
    ips = []
    with open('access.log') as f:
        for line in f:
            ips.append(line.split()[0])
    
    print(ips)
    print("PV is {0}".format(len(ips)))
    print("UV is {0}".format(len(set(ips))))
    from __future__ import print_function
    from collections import Counter
    
    c = Counter()
    with open('access.log') as f:
        for line in f:
            c[line.split()[6]] += 1
    
    print(c)
    print("Popular resources : {0}".format(c.most_common(10)))
    from __future__ import print_function
    from collections import Counter
    
    d = {}
    with open('access.log') as f:
        for line in f:
            key = line.split()[8]
            d.setdefault(key, 0)
            d[key] += 1
    
    sum_requests = 0
    error_requests = 0
    
    for key, val in d.items():
        if int(key) >= 400:
            error_requests += val
        sum_requests += val
    
    print("error rate: {0:.2f}%".format(error_requests * 100.0 / sum_requests))

     4).字符串格式化

        在Python中,存在两种格式化字符串的方法,即%表达式和format函数

        format函数才是字符串格式化的未来

        "{} is better than {}.".format('Beautiful', 'ugly')

    2.正则表达式

    1).利用re库处理正则表达式

    非编译的正则表达式版本:
    import re
    
    def main():
        pattern = "[0-9]+"
        with open('data.txt') as f:
            for line in f:
                re.findall(pattern, line)
    
    if __name__ == '__main__':
        main()
    编译的正则表达式版本:
    import re
    
    def main():
        pattern = "[0-9]+"
        re_obj = re.complile(pattern)
        with open('data.txt') as f:
            for line in f:
                re_obj.findall(line)
    
    if __name__ == '__main__':
        main()

         search函数与match函数用法几乎一样,区别在于前者在字符串的任意位置进行匹配,后者仅对字符串的开始部分进行匹配

    2).常用的re方法

        匹配类函数:re模块中最简单的便是findall函数

        修改类函数:re模块的sub函数类似于字符串的replace函数,只是sub函数支持正则表达式

    3).案例:获取HTML页面中的所有超链接

    import re
    import requests
    
    r = requests.get('https://news.ycombinator.com/')
    mydata = re.findall('"(https?://.*?)"', r.content.decode('utf-8'))
    print(mydata)

    3.字符集编码

    1).UTF8编码

    2).Python2和Python3中的Unicode

        把Unicode字符表示为二进制数据有许多中办法,最常见的编码方式就是UTF-8

        Unicode是表现形式,UTF-8是存储形式

        UTT-8是使用最广泛的编码,但仅仅是Unicode的一种存储形式

        from __future__ import unicode_literals

    4.Jiaja2模板

    1).语法块

    在Jinja2中,存在三种语法:
    控制结构{%%}
    变量取值{{}}
    注释{##}
    {% if users %}
        <ul>
        {% for user in users %}
            <li>{{ user.username }}</li>
        {% endfor %}
        </ul>
    {% endif %}

    2).Jinja2的继承和Super函数

    base.html页面
    <html lang= "en" >
    <head>
        {% block head %<link rel ="stylesheet" href="style.css" />
        <title> {% block title %}{% endblock %}- My Webpage</title>
        {% endblock %</ head>
    <body>
    <div id="content">% block content %}{% endblock %}
    </div>
    </body >
    index.html页面
    {% extends "base .html" %}
    {% block title %}index{% endblock %}
    {% block head %}
    { { super () } }
    <style type= ” text/css ” >
    .important { color: #336699 ; }
    </style>
    {% endblock %}
    {% block content %<hl >index</hl>
    <p class"important"> Welcome on my awesome homepage. </p>
    {% endblock %}
  • 相关阅读:
    今天的赢在中国推迟了,给大家推荐个视频,看看什么是真正的中华武术
    赢在中国080312
    盛大(上海)诚聘软件测试人员
    jQuery入门[2]-选择器
    阿里巴巴诚信通成为《赢在中国》的实战项目,要花多少钱?--《赢在中国》(20080408)
    唐僧晒书
    用于生成网页、WAP、JS中文编码的Unicode编码工具
    jQuery入门[5]-AJAX
    中国哲学(一)
    20071212公映的《色即是空2》(sex is zero 2)字幕文件
  • 原文地址:https://www.cnblogs.com/allenhu320/p/11326342.html
Copyright © 2011-2022 走看看