python学习笔记14 (正则表达式2) 2018-05-03

今天主要是以2道习题的为主,加强对正则表达式的理解:

一,获取json中的skuid和skuimgurl的值:

"errCode":"0",
    "retCode":"",
    "msg":"success",
    "size":"20",
    "sku":[
        {
            "skuid":"26878432382",
            "spuid":"26878432382",
            "skuurl":"[https://qitem.jd.com/fcgi-bin/view?sku=26878432382&shopid=0&enc=jn6CsRbAaCZmUGvwesX4UtzRM1_Urt4t3rrpcBqQFCaOb20E9H_1lIDZrnjpTnQxpDyV61PjwLJntzDdBK6Ws0stJnT97ZXn5YoMHMfdHIXg551U4dquFQ%3D%3D&tk=b532cffc1b0581ce0b3da372aacc8c1d&share=1&fs=1>=1525352327&spu=26878432382&v=1](https://qitem.jd.com/fcgi-bin/view?sku=26878432382&shopid=0&enc=jn6CsRbAaCZmUGvwesX4UtzRM1_Urt4t3rrpcBqQFCaOb20E9H_1lIDZrnjpTnQxpDyV61PjwLJntzDdBK6Ws0stJnT97ZXn5YoMHMfdHIXg551U4dquFQ%3D%3D&tk=b532cffc1b0581ce0b3da372aacc8c1d&share=1&fs=1%3E=1525352327&spu=26878432382&v=1)",
            "skuimgurl":"[https://img12.360buyimg.com/n7/jfs/t18226/169/1318243724/390477/5b0718ff/5ac44edcNa350dbd9.jpg](https://img12.360buyimg.com/n7/jfs/t18226/169/1318243724/390477/5b0718ff/5ac44edcNa350dbd9.jpg)",
            "title":"丹龙(DanLong)声波电动牙刷成人家用非充电式智能防水情侣牙刷 牛奶白-标配",
            "price":"129.00",
            "commissionprice":"25.80",
            "salecount":"6551",
            "goodCom":"3031",
            "comRate":"0.2000"
        },
       ...
image.png

首先,我们在线https://regex101.com/对正则表达式检测:如图

image.png

获取我们需要的正则表达式\"skuid\S{3}(\d+)\S.*?skuimgurl\S{3}(.*?jpg)\"
image.png

然后使用findall()方法得到匹配成功的list;

二,对文件内容匹配并相对应的生成文件:

有一个ga10.wms5.jd.com.txt 文件:内容一下


upstream orderCenter.ga10.wms5.jd.local {
    server 10.46.0.161:8023 weight=10 max_fails=2 fail_timeout=30s;
    server 10.46.0.162:8023 weight=10 max_fails=2 fail_timeout=30s;
}

upstream opperftrace.ga10.wms5.jd.local {
    server 10.46.0.164:8060 weight=10 max_fails=2 fail_timeout=30s;
}

upstream taskassign-c.ga10.wms5.jd.local {
    server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=30s;
}

upstream smartQuery.ga10.wms5.jd.local {
    server 10.46.0.164:8013 weight=10 max_fails=2 fail_timeout=30s;
}

upstream center.ga10.wms5.jd.local {
    server 10.46.0.164:9020 weight=10 max_fails=2 fail_timeout=30s;
    server 10.46.0.163:9020 weight=10 max_fails=2 fail_timeout=30s;
}
upstream aps.wms5.jd.local {
  server 10.46.0.161:8001 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream inbound.wms5.jd.local {
  server 10.46.0.161:8002 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8002 weight=10 max_fails=2 fail_timeout=10s;
}
upstream invop.wms5.jd.local {
  server 10.46.0.161:8003 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8003 weight=10 max_fails=2 fail_timeout=10s;
}
upstream mcs.wms5.jd.local {
  server 10.46.0.161:8004 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8004 weight=10 max_fails=2 fail_timeout=10s;
}
upstream pickingplan.wms5.jd.local {
  server 10.46.0.161:8005 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=10s;
}
upstream picking.wms5.jd.local {
  server 10.46.0.161:8006 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8006 weight=10 max_fails=2 fail_timeout=10s;
}
upstream power.wms5.jd.local {
  server 10.46.0.161:8007 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8007 weight=10 max_fails=2 fail_timeout=10s;
}
upstream shipment.wms5.jd.local {
  server 10.46.0.161:8008 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8008 weight=10 max_fails=2 fail_timeout=10s;
}
upstream stock.wms5.jd.local {
  server 10.46.0.161:8009 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8009 weight=10 max_fails=2 fail_timeout=10s;
}
upstream inner.wms5.jd.local {
  server 10.46.0.161:8011 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8010 weight=10 max_fails=2 fail_timeout=10s;
}
upstream task.wms5.jd.local {
  server 10.46.0.163:8001 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream taskmg.wms5.jd.local {
  server 10.46.0.163:8002 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8002 weight=10 max_fails=2 fail_timeout=10s;
}
upstream report.wms5.jd.local {
  server 10.46.0.163:8003 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8003 weight=10 max_fails=2 fail_timeout=10s;
}
upstream master.wms5.jd.local {
  server 10.46.0.163:8004 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8004 weight=10 max_fails=2 fail_timeout=10s;
}
upstream clover.jd.local {
  server 10.46.0.163:1601 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:1601 weight=10 max_fails=2 fail_timeout=10s;
}

upstream backbone.web.wms5.jd.local {
  server 10.46.0.163:8006 weight=10 max_fails=2 fail_timeout=10s;
}
upstream wump-heartbeat.wms5.jd.local {
    server 10.46.0.130:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream dec.wms5.jd.local {
        server 10.46.0.161:8012 weight=10 max_fails=2 fail_timeout=10s;
        server 10.46.0.162:8011 weight=10 max_fails=2 fail_timeout=10s;
}


# ##############################################################################

server
{
    listen                   80;
    server_name              ga10.wms5.jd.com 10.46.0.217 10.46.0.161;
    access_log               /export/servers/nginx/logs/ga10.wms5.jd.com/ga10.wms5.jd.com_access.log main;
    error_log                /export/servers/nginx/logs/ga10.wms5.jd.com/ga10.wms5.jd.com_error.log warn;
    #chunkin on;
    error_page 411 = @my_error;
    location @my_error {
        #chunkin_resume;
    }


        location /logs/ {
                autoindex       off;
                deny all;
        }

    # frontend  ########################################
    rewrite_log on;
    #more_set_headers "Foo: bar";
location /dec/ {
                proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
                proxy_set_header        Host  $host;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                expires                 0;
                proxy_pass http://dec.wms5.jd.local/;
        }
location ~ /pickingplan/((?:services/)?taskassign_.+) {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    rewrite /pickingplan/((?:services/)?taskassign_.+) /$1 break;
    proxy_pass http://taskassign-c.ga10.wms5.jd.local;
}

location /wump-heartbeat/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://wump-heartbeat.wms5.jd.local/;
}
location /master/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://master.wms5.jd.local/;
}
location /mcs/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://mcs.wms5.jd.local/;
}
location /power/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://power.wms5.jd.local/;
}
location /cluster1/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://clover.jd.local;
}
location /aps/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://aps.wms5.jd.local/;
}
location /task/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://task.wms5.jd.local/;
}
location /taskmg/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://taskmg.wms5.jd.local/;
}
location /stock/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://stock.wms5.jd.local/;
}
location /invop/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://invop.wms5.jd.local/;
}
location /picking/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://picking.wms5.jd.local/;
}
location /pickingplan/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://pickingplan.wms5.jd.local/;
}
location /shipment/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://shipment.wms5.jd.local/;
}
location /inbound/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://inbound.wms5.jd.local/;
}
location /winapp/ {
    root /export/App/app.wms5.jd.local/;
    index ReleaseList.xml;
}   
location /rfapp/ {
    root /export/App/app.wms5.jd.local/;
    index ReleaseList.xml;  
}   
location /report/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://report.wms5.jd.local/;
}
location /inner/ {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    proxy_pass http://inner.wms5.jd.local/;
}
location /backbone/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://backbone.web.wms5.jd.local/;
}

    location /center/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://center.ga10.wms5.jd.local/;
    }

    location /smartQuery/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://smartQuery.ga10.wms5.jd.local/;
    }

    location /opperftrace/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://opperftrace.ga10.wms5.jd.local/;
    }

    location /orderCenter/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://orderCenter.ga10.wms5.jd.local/;
    }
}

(1) 获取upstream后面的内容 相对应的生成文件,将其包含的内容写入文件中:

import codecs
import re
import os

# 匹配出文件名的正则表达式  
'''
第一个()匹配出upstream的所有内容:upstream aps.wms5.jd.local {
  server 10.46.0.161:8001 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8001 weight=10 max_fails=2 fail_timeout=10s;
}
第二个()匹配出upstream后面的文件名aps.wms5.jd.local
'''
regUpstream = re.compile(r"\s*(upstream\s+(\S+)\s+{[^}]+})")

# 打开文件
with codecs.open("ga10.wms5.jd.com.txt") as fu:
    # 正则匹配获取文件名
    textList = regUpstream.findall(fu.read())

    # 创建文件
    if not os.path.exists("upstream"):
        os.mkdir("upstream")  # 不存在就创建upstreamwen文件夹

    os.chdir("upstream")  # 切换到新建文件夹下
    for item in textList:
        with codecs.open(item[1],"w") as fw:
            fw.write(item[0])
    os.chdir("..")

运行结果:


image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,794评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,050评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,587评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,861评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,901评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,898评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,832评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,617评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,077评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,349评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,483评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,199评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,824评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,442评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,632评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,474评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,393评论 2 352

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,649评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,047评论 25 707
  • python的re模块--细说正则表达式 可能是东半球最详细最全面的re教程,翻译自官方文档,因为官方文档写的是真...
    立而人阅读 22,868评论 4 46
  • 温柔的灯光 笼罩夜的静谧 这一室 一床 满满都是你的气息 你在那里 静静地阅读 匆匆地书写 投入地游戏 我 辗转...
    梦言梦语阅读 344评论 0 0
  • 自己是从什么时候开始,变得如此没有自制能力了呢;自己是从什么时候开始,变得浑浑噩噩混日子了呢;自己是从什么时候开始...
    木欢阅读 773评论 1 2