生活随笔
收集整理的這篇文章主要介紹了
四、正则表达式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、正則表達(dá)式的概念和作用
正則表達(dá)式概念:一種字符串匹配的模式
正則表達(dá)式作用:
可以檢查一個字符串中是否包含某種字串替換匹配的字串提取某個字符串中匹配的字串
二、正則表達(dá)式中常見的語法
字符描述原樣字符匹配字符
| 一般字符 | 匹配自身 | beyond | beyond |
| . | 匹配任意除換行符\n以外的字符 | a.c | aac或abc或acc |
| \ | 轉(zhuǎn)義字符,使后一個字符改變原來的意思。如果字符串中有字符*需要匹配,可以使用\*或者字符集[*] | a\.c、a\\c | a.c、a\c |
| [...] | 字符集(字符類)。對應(yīng)的位置可以是字符集中任意字符。字符集中的字符可以逐個列出,也可以給出范圍,如[abc]或[a-c]。第一個字符如果是^則表示取反,如[^abc] 表示不是abc的其他字符。所有的特殊字符在字符集中都失去其原有的特殊含義。在字符集中如果要使用]、-、^,可以在前面加上反斜杠,或把]、-放在第一個字符,把^放在非第一個字符 | a[bcd]e | abe、ace、ade |
import re
rs1
= re
.findall
('abc','beyondabcbeyondyanyu')
print(rs1
)rs2
= re
.findall
('a.c','111a*c')
print(rs2
)rs3
= re
.findall
('a.c','111abc')
print(rs3
)rs4
= re
.findall
('a.c','222a.c')
print(rs4
)rs5
= re
.findall
('a.c','a\nc')
print(rs5
)rs6
= re
.findall
('a\.c','111a.c')
print(rs6
)rs7
= re
.findall
('a\.c','111abc')
print(rs7
)rs8
= re
.findall
('a[bd]c','abcqq')
print(rs8
)rs9
= re
.findall
('a[bd]c','adcq')
print(rs9
)rs0
= re
.findall
('a[bd]c','afc')
print(rs0
)
預(yù)定義字符集(可以寫在字符集[…]中)
每個預(yù)定義字符集中的字符對應(yīng)一個字符,中括號里面可以有很多預(yù)定義字符集,表示任滿足其一即可。
字符描述原樣字符匹配字符
| \d | 數(shù)字:[0-9] | a\dc | a5c或a2c或a9c |
| \D | 非數(shù)字:[^\d] | a\Dc | abc或ayc或a*c |
| \s | 空白字符:[<空格>\t\r\n\f\v] | a\sc | a c |
| \S | 非空白字符:[^\s] | a\Sc | abc或a8c或a*c |
| \w | 單詞字符:[A-Za-z0-9_] | a\wc | abc或aDc或a8c |
| \W | 非單詞字符:[^\w] | a\Wc | a c或a&c或a)c |
import re
rs1
= re
.findall
('\d','q1s74d')
print(rs1
)rs2
= re
.findall
('\w','q!#1s7_4d$')
print(rs2
)rs3
= re
.findall
('[\s\d]','q 1s7_ 4d $')
print(rs3
)
數(shù)量詞(用在字符或(…)之后)
字符描述原樣字符匹配字符
| * | 匹配前一個字符零次或無限次 | abc* | ab、abccccc |
| + | 匹配前一個字符一次或無限次 | abc+ | abc、abcccccc |
| ? | 匹配前一個字符零次或一次 | abc? | ab、abc |
| {m} | 匹配前一個字符m次 | ab{2}c | abbc |
import re
rs1
= re
.findall
('y\d*','ay0522y12y24yy')
print(rs1
)rs2
= re
.findall
('y\d+','bey12ond0522yan123yu')
print(rs2
)rs3
= re
.findall
('y\d?','ty12ond0522y3an123yu')
print(rs3
)rs4
= re
.findall
('y\d{3}','ty1332ond0522y3any123yu')
print(rs4
)
三、re.findall()方法
re.findall(pattern,string,flags=0)
掃描整個string字符串,返回所有與pattern匹配的列表
參數(shù):
- pattern:正則表達(dá)式
- string:從哪個字符串中查找
- flags:匹配模式
返回:
- 返回string中與pattern匹配的結(jié)果列表
例如:
- re.findall("\d",“b1e1y2o3n4d”)
- 返回結(jié)果為[“1”,“1”,“2”,“3”,“4”]
1,findall方法返回匹配的結(jié)果列表
import rers1
= re
.findall
('\d','b12e3y4o56n777d')
print(rs1
)rs2
= re
.findall
('\d+','b12e3y4o56n777d')
print(rs2
)
2,findall方法中flag參數(shù)的作用
re.DOTALL、re.S
import rers1
= re
.findall
('a.bc','a\nbc')
print(rs1
)rs2
= re
.findall
('a.bc','a\nbc',re
.DOTALL
)
print(rs2
)rs3
= re
.findall
('a.bc','a\nbc',re
.S
)
print(rs3
)
3,findall方法中分組的使用
分組使用小括號表示
這里的a(.+)bc,即先通過前a,后bc確定位置,之后再返回(.+)匹配的內(nèi)容
import rers1
= re
.findall
('a.+bc','yya\nbc',re
.DOTALL
)
print(rs1
)rs2
= re
.findall
('a(.+)bc','yya\nbc',re
.DOTALL
)
print(rs2
)
如果正則表達(dá)式中沒有(),則返回與整個正則表達(dá)式匹配的列表
如果正則表達(dá)式中有(),則返回()中匹配的內(nèi)容列表,小括號兩邊的東西都是負(fù)責(zé)確定提取數(shù)據(jù)的所在位置
四、正則表達(dá)式中r原串的使用
r原串實際上是為了解決轉(zhuǎn)義符問題
import rers1
= re
.findall
('a\nbc','a\nbc')
print(rs1
)rs2
= re
.findall
('a\\nbc','a\\nbc')
print(rs2
)rs3
= re
.findall
('a\\\nbc','a\\nbc')
print(rs3
)rs4
= re
.findall
('a\\\\nbc','a\\nbc')
print(rs4
)rs5
= re
.findall
(r'a\nbc','a\nbc')
print(rs5
)rs6
= re
.findall
(r'a\\nbc','a\\nbc')
print(rs6
)
五、提取最新的疫情數(shù)據(jù)的json字符串
步驟:
請求疫情首頁內(nèi)容提取script標(biāo)簽中各國的疫情信息從各國疫情信息中提取各國疫情的json字符串
當(dāng)然,數(shù)據(jù)來源仍然是丁香園新型冠狀病毒肺炎疫情實時動態(tài)首頁
url:https://ncov.dxy.cn/ncovh5/view/pneumonia
import requests
import re
from bs4
import BeautifulSoup
response
= requests
.get
('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page
= response
.content
.decode
()
soup
= BeautifulSoup
(home_page
,'lxml')
script
= soup
.find
(id='getAreaStat')
text
= script
.text
'''
try { window.getAreaStat = [{"provinceName":"香港","provinceShortName":"香港","currentConfirmedCount":5990,"confirmedCount":22468,"suspectedCount":181,"curedCount":16190,"deadCount":288,"comment":"疑似 1 例","locationId":810000,"statisticsData":"https://file1.dxycdn.com/2020/0223/331/3398299755968040033-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":0,"vaccinationOrgCount":0,"cities":[],"dangerAreas":[]},{"provinceName":"臺灣","provinceShortName":"臺灣","currentConfirmedCount":5413,"confirmedCount":20007,"suspectedCount":485,"curedCount":13742,"deadCount":852,"comment":"","locationId":710000,"statisticsData":"https://file1.dxycdn.com/2020/0223/045/3398299749526003760-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":0,"vaccinationOrgCount":0,"cities":[],"dangerAreas":[]},{"provinceName":"浙江省","provinceShortName":"浙江","currentConfirmedCount":388,"confirmedCount":2255,"suspectedCount":68,"curedCount":1866,"deadCount":1,"comment":"2月10日通報核減的12例在浙江省治愈的外省病例,根據(jù)國家最新要求重新納入累計病例。","locationId":330000,"statisticsData":"https://file1.dxycdn.com/2020/0223/537/3398299755968455045-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":519,"vaccinationOrgCount":217,"cities":[{"cityName":"杭州","currentConfirmedCount":143,"confirmedCount":328,"suspectedCount":0,"curedCount":185,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330100,"currentConfirmedCountStr":"143"},{"cityName":"境外輸入","currentConfirmedCount":119,"confirmedCount":387,"suspectedCount":68,"curedCount":268,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"119"},{"cityName":"寧波","currentConfirmedCount":110,"confirmedCount":269,"suspectedCount":0,"curedCount":159,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330200,"currentConfirmedCountStr":"110"},{"cityName":"紹興","currentConfirmedCount":38,"confirmedCount":430,"suspectedCount":0,"curedCount":392,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330600,"currentConfirmedCountStr":"38"},{"cityName":"金華","currentConfirmedCount":2,"confirmedCount":57,"suspectedCount":0,"curedCount":55,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330700,"currentConfirmedCountStr":"2"},{"cityName":"溫州","currentConfirmedCount":0,"confirmedCount":504,"suspectedCount":0,"curedCount":503,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":330300,"currentConfirmedCountStr":"0"},{"cityName":"臺州","currentConfirmedCount":0,"confirmedCount":147,"suspectedCount":0,"curedCount":147,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":331000,"currentConfirmedCountStr":"0"},{"cityName":"嘉興","currentConfirmedCount":0,"confirmedCount":46,"suspectedCount":0,"curedCount":46,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330400,"currentConfirmedCountStr":"0"},{"cityName":"省十里豐監(jiān)獄","currentConfirmedCount":0,"confirmedCount":36,"suspectedCount":0,"curedCount":36,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"0"},{"cityName":"麗水","currentConfirmedCount":0,"confirmedCount":17,"suspectedCount":0,"curedCount":17,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":331100,"currentConfirmedCountStr":"0"},{"cityName":"衢州","currentConfirmedCount":0,"confirmedCount":14,"suspectedCount":0,"curedCount":14,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330800,"currentConfirmedCountStr":"0"},{"cityName":"湖州","currentConfirmedCount":0,"confirmedCount":10,"suspectedCount":0,"curedCount":10,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330500,"currentConfirmedCountStr":"0"},{"cityName":"舟山","currentConfirmedCount":0,"confirmedCount":10,"suspectedCount":0,"curedCount":10,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330900,"currentConfirmedCountStr":"0"},{"cityName":"待明確地區(qū)","currentConfirmedCount":-24,"confirmedCount":0,"suspectedCount":0,"curedCount":24,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"notShowCurrentConfirmedCount":true,"currentConfirmedCountStr":"-"}],"dangerAreas":[]},{"provinceName":"廣東省","provinceShortName":"廣東","currentConfirmedCount":362,"confirmedCount":4163,"suspectedCount":25,"curedCount":3793,"deadCount":8,"comment":"廣東衛(wèi)健委未明確部分治愈病例的地市歸屬,因此各地市的現(xiàn)存確診存在一定偏差。","locationId":440000,"statisticsData":"https://file1.dxycdn.com/2020/0223/281/3398299758115524068-135.json","highDangerCount":0,"midDangerCount":3,"detectOrgCount":120,"vaccinationOrgCount":42,"cities":[{"cityName":"深圳","currentConfirmedCount":145,"confirmedCount":798,"suspectedCount":3,"curedCount":650,"deadCount":3,"highDangerCount":0,"midDangerCount":3,"locationId":440300,"currentConfirmedCountStr":"145"},{"cityName":"廣州","currentConfirmedCount":103,"confirmedCount":2205,"suspectedCount":3,"curedCount":2101,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":440100,"currentConfirmedCountStr":"103"},{"cityName":"東莞","currentConfirmedCount":31,"confirmedCount":202,"suspectedCount":1,"curedCount":170,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":441900,"currentConfirmedCountStr":"31"},{"cityName":"佛山","currentConfirmedCount":28,"confirmedCount":318,"suspectedCount":1,"curedCount":290,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":440600,"currentConfirmedCountStr":"28"},{"cityName":"陽江","currentConfirmedCount":23,"confirmedCount":51,"suspectedCount":0,"curedCount":28,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441700,"currentConfirmedCountStr":"23"},{"cityName":"珠海","currentConfirmedCount":15,"confirmedCount":169,"suspectedCount":2,"curedCount":153,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":440400,"currentConfirmedCountStr":"15"},{"cityName":"惠州","currentConfirmedCount":7,"confirmedCount":71,"suspectedCount":0,"curedCount":64,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441300,"currentConfirmedCountStr":"7"},{"cityName":"江門","currentConfirmedCount":7,"confirmedCount":47,"suspectedCount":0,"curedCount":40,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":440700,"currentConfirmedCountStr":"7"},{"cityName":"云浮","currentConfirmedCount":7,"confirmedCount":7,"suspectedCount":0,"curedCount":0,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":445300,"currentConfirmedCountStr":"7"},{"cityName":"中山","currentConfirmedCount":4,"confirmedCount":80,"suspectedCount":0,"curedCount":76,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":442000,"currentConfirmedCountStr":"4"},{"cityName":"湛江","currentConfirmedCount":2,"confirmedCount":43,"suspectedCount":2,"curedCount":41,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":440800,"currentConfirmedCountStr":"2"},{"cityName":"河源","currentConfirmedCount":1,"confirmedCount":6,"suspectedCount":0,"curedCount":5,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441600,"currentConfirmedCountStr":"1"},{"cityName":"肇慶","currentConfirmedCount":0,"confirmedCount":47,"suspectedCount":1,"curedCount":46,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":441200,"currentConfirmedCountStr":"0"},{"cityName":"汕頭","currentConfirmedCount":0,"confirmedCount":26,"suspectedCount":0,"curedCount":26,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":440500,"currentConfirmedCountStr":"0"},{"cityName":"清遠(yuǎn)","currentConfirmedCount":0,"confirmedCount":23,"suspectedCount":0,"curedCount":23,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441800,"currentConfirmedCountStr":"0"},{"cityName":"梅州","currentConfirmedCount":0,"confirmedCount":19,"suspectedCount":0,"curedCount":19,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441400,"currentConfirmedCountStr":"0"},{"cityName":"茂名","currentConfirmedCount":0,"confirmedCount":17,"suspectedCount":0,"curedCount":17,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":440900,"currentConfirmedCountStr":"0"},{"cityName":"揭陽","currentConfirmedCount":0,"confirmedCount":11,"suspectedCount":0,"curedCount":11,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":445200,"currentConfirmedCountStr":"0"},{"cityName":"韶關(guān)","currentConfirmedCount":0,"confirmedCount":10,"suspectedCount":0,"curedCount":9,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":440200,"currentConfirmedCountStr":"0"},{"cityName":"潮州","currentConfirmedCount":0,"confirmedCount":7,"suspectedCount":0,"curedCount":7,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":445100,"currentConfirmedCountStr":"0"},{"cityName":"汕尾","currentConfirmedCount":0,"confirmedCount":6,"suspectedCount":0,"curedCount":6,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":441500,"currentConfirmedCountStr":"0"},{"cityName":"待明確地區(qū)","currentConfirmedCount":-11,"confirmedCount":0,"suspectedCount":0,"curedCount":11,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"notShowCurrentConfirmedCount":true,"currentConfirmedCountStr":"-"}],"dangerAreas":[{"cityName":"深圳","areaName":"龍崗區(qū)坂田街道馬安堂社區(qū)僑聯(lián)東10巷1號順興樓","dangerLevel":2},{"cityName":"深圳","areaName":"羅湖區(qū)東門街道新園路明華廣場1至6樓(含6A與M層)商業(yè)區(qū)","dangerLevel":2},{"cityName":"深圳","areaName":"中興路高時石材B區(qū)A鋼構(gòu)廠房","dangerLevel":2}]},{"provinceName":"廣西壯族自治區(qū)","provinceShortName":"廣西","currentConfirmedCount":319,"confirmedCount":1028,"suspectedCount":0,"curedCount":707,"deadCount":2,"comment":"","locationId":450000,"statisticsData":"https://file1.dxycdn.com/2020/0223/536/3398299758115523880-135.json","highDangerCount":1,"midDangerCount":10,"detectOrgCount":270,"vaccinationOrgCount":15,"cities":[{"cityName":"百色","currentConfirmedCount":227,"confirmedCount":274,"suspectedCount":0,"curedCount":47,"deadCount":0,"highDangerCount":1,"midDangerCount":10,"locationId":451000,"currentConfirmedCountStr":"227"},{"cityName":"境外輸入","currentConfirmedCount":91,"confirmedCount":482,"suspectedCount":0,"curedCount":391,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"91"},{"cityName":"南寧","currentConfirmedCount":1,"confirmedCount":57,"suspectedCount":0,"curedCount":56,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450100,"currentConfirmedCountStr":"1"},{"cityName":"北海","currentConfirmedCount":0,"confirmedCount":44,"suspectedCount":0,"curedCount":43,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":450500,"currentConfirmedCountStr":"0"},{"cityName":"防城港","currentConfirmedCount":0,"confirmedCount":39,"suspectedCount":0,"curedCount":39,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450600,"currentConfirmedCountStr":"0"},{"cityName":"桂林","currentConfirmedCount":0,"confirmedCount":32,"suspectedCount":0,"curedCount":32,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450300,"currentConfirmedCountStr":"0"},{"cityName":"河池","currentConfirmedCount":0,"confirmedCount":28,"suspectedCount":0,"curedCount":27,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":451200,"currentConfirmedCountStr":"0"},{"cityName":"柳州","currentConfirmedCount":0,"confirmedCount":24,"suspectedCount":0,"curedCount":24,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450200,"currentConfirmedCountStr":"0"},{"cityName":"玉林","currentConfirmedCount":0,"confirmedCount":11,"suspectedCount":0,"curedCount":11,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450900,"currentConfirmedCountStr":"0"},{"cityName":"來賓","currentConfirmedCount":0,"confirmedCount":11,"suspectedCount":0,"curedCount":11,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":451300,"currentConfirmedCountStr":"0"},{"cityName":"欽州","currentConfirmedCount":0,"confirmedCount":8,"suspectedCount":0,"curedCount":8,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450700,"currentConfirmedCountStr":"0"},{"cityName":"貴港","currentConfirmedCount":0,"confirmedCount":8,"suspectedCount":0,"curedCount":8,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450800,"currentConfirmedCountStr":"0"},{"cityName":"梧州","currentConfirmedCount":0,"confirmedCount":5,"suspectedCount":0,"curedCount":5,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":450400,"currentConfirmedCountStr":"0"},{"cityName":"賀州","currentConfirmedCount":0,"confirmedCount":4,"suspectedCount":0,"curedCount":4,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":451100,"currentConfirmedCountStr":"0"},{"cityName":"崇左","currentConfirmedCount":0,"confirmedCount":1,"suspectedCount":0,"curedCount":1,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":451400,"currentConfirmedCountStr":"0"}],"dangerAreas":[{"cityName":"百色","areaName":"德保縣都安鄉(xiāng)伏計村隴意屯","dangerLevel":1},{"cityName":"百色","areaName":"城關(guān)鎮(zhèn)隆盛社區(qū)東蒙榮盛二巷25號","dangerLevel":2},{"cityName":"百色","areaName":"城關(guān)鎮(zhèn)隆盛社區(qū)盛象名都小區(qū)","dangerLevel":2},{"cityName":"百色","areaName":"都安鄉(xiāng)坡那村多麥屯","dangerLevel":2},{"cityName":"百色","areaName":"德保縣都安鄉(xiāng)福記村山金屯","dangerLevel":2},{"cityName":"百色","areaName":"德保縣維也納酒店(德保騰飛廣場店)","dangerLevel":2},{"cityName":"百色","areaName":"東凌鎮(zhèn)登限村念洞屯","dangerLevel":2},{"cityName":"百色","areaName":"敬德鎮(zhèn)隴正村多果屯","dangerLevel":2},{"cityName":"百色","areaName":"靖西市武平鎮(zhèn)大道街大定屯","dangerLevel":2},{"cityName":"百色","areaName":"蓮城社區(qū)德立山莊","dangerLevel":2},{"cityName":"百色","areaName":"弄貼村新村屯","dangerLevel":2}]},{"provinceName":"上海市","provinceShortName":"上海","currentConfirmedCount":209,"confirmedCount":4003,"suspectedCount":393,"curedCount":3787,"deadCount":7,"comment":"因未公布分區(qū)死亡和治愈,僅展示累計確診和現(xiàn)存確診","locationId":310000,"statisticsData":"https://file1.dxycdn.com/2020/0223/128/3398299755968454977-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":130,"vaccinationOrgCount":17,"cities":[{"cityName":"境外輸入","currentConfirmedCount":208,"confirmedCount":3611,"suspectedCount":8,"curedCount":3403,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"208"},{"cityName":"奉賢區(qū)","currentConfirmedCount":1,"confirmedCount":11,"suspectedCount":0,"curedCount":10,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":310120,"currentConfirmedCountStr":"1"},{"cityName":"外地來滬","currentConfirmedCount":0,"confirmedCount":113,"suspectedCount":0,"curedCount":112,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"0"},{"cityName":"浦東新區(qū)","currentConfirmedCount":0,"confirmedCount":82,"suspectedCount":0,"curedCount":81,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":310115,"currentConfirmedCountStr":"0"},{"cityName":"寶山區(qū)","currentConfirmedCount":0,"confirmedCount":27,"suspectedCount":0,"curedCount":26,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":310113,"currentConfirmedCountStr":"0"},{"cityName":"黃浦區(qū)","currentConfirmedCount":0,"confirmedCount":22,"suspectedCount":0,"curedCount":22,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":310101,"currentConfirmedCountStr":"0"},{"cityName":"閔行區(qū)","currentConfirmedCount":0,"confirmedCount":19,"suspectedCount":0,"curedCount":19,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":310112,"currentConfirmedCountStr":"0"},{"cityName":"徐匯區(qū)","currentConfirmedCount":0,"confirmedCount":18,"suspectedCount":0,"curedCount":17,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":310104,"currentConfirmedCountStr":"0"},{"cityName":"靜安區(qū)","currentConfirmedCount":0,"confirmedCount":17,"suspectedCount":0,"curedCount":16,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":310106,"currentConfirmedCountStr":"0"},{"cityName":"松江區(qū)","currentConfirmedCount":0,"confirmedCount":16,"suspectedCount":0,"curedCount":16,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":310117,"currentConfirmedCountStr":"0"},{"cityName":"長寧區(qū)","currentConfirmedCount":0,"confirmedCount":14,"suspectedCount":0,"curedCount":14,"deadCount":...內(nèi)容太多了已省略
'''
json_str
= re
.findall
(r'\[.+\]',text
)[0]
print(json_str
)
'''
[{"provinceName":"香港","provinceShortName":"香港","currentConfirmedCount":5990,"confirmedCount":22468,"suspectedCount":181,"curedCount":16190,"deadCount":288,"comment":"疑似 1 例","locationId":810000,"statisticsData":"https://file1.dxycdn.com/2020/0223/331/3398299755968040033-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":0,"vaccinationOrgCount":0,"cities":[],"dangerAreas":[]},{"provinceName":"臺灣","provinceShortName":"臺灣","currentConfirmedCount":5413,"confirmedCount":20007,"suspectedCount":485,"curedCount":13742,"deadCount":852,"comment":"","locationId":710000,"statisticsData":"https://file1.dxycdn.com/2020/0223/045/3398299749526003760-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":0,"vaccinationOrgCount":0,"cities":[],"dangerAreas":[]},{"provinceName":"浙江省","provinceShortName":"浙江","currentConfirmedCount":388,"confirmedCount":2255,"suspectedCount":68,"curedCount":1866,"deadCount":1,"comment":"2月10日通報核減的12例在浙江省治愈的外省病例,根據(jù)國家最新要求重新納入累計病例。","locationId":330000,"statisticsData":"https://file1.dxycdn.com/2020/0223/537/3398299755968455045-135.json","highDangerCount":0,"midDangerCount":0,"detectOrgCount":519,"vaccinationOrgCount":217,"cities":[{"cityName":"杭州","currentConfirmedCount":143,"confirmedCount":328,"suspectedCount":0,"curedCount":185,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330100,"currentConfirmedCountStr":"143"},{"cityName":"境外輸入","currentConfirmedCount":119,"confirmedCount":387,"suspectedCount":68,"curedCount":268,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"119"},{"cityName":"寧波","currentConfirmedCount":110,"confirmedCount":269,"suspectedCount":0,"curedCount":159,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330200,"currentConfirmedCountStr":"110"},{"cityName":"紹興","currentConfirmedCount":38,"confirmedCount":430,"suspectedCount":0,"curedCount":392,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330600,"currentConfirmedCountStr":"38"},{"cityName":"金華","currentConfirmedCount":2,"confirmedCount":57,"suspectedCount":0,"curedCount":55,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330700,"currentConfirmedCountStr":"2"},{"cityName":"溫州","currentConfirmedCount":0,"confirmedCount":504,"suspectedCount":0,"curedCount":503,"deadCount":1,"highDangerCount":0,"midDangerCount":0,"locationId":330300,"currentConfirmedCountStr":"0"},{"cityName":"臺州","currentConfirmedCount":0,"confirmedCount":147,"suspectedCount":0,"curedCount":147,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":331000,"currentConfirmedCountStr":"0"},{"cityName":"嘉興","currentConfirmedCount":0,"confirmedCount":46,"suspectedCount":0,"curedCount":46,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":330400,"currentConfirmedCountStr":"0"},{"cityName":"省十里豐監(jiān)獄","currentConfirmedCount":0,"confirmedCount":36,"suspectedCount":0,"curedCount":36,"deadCount":0,"highDangerCount":0,"midDangerCount":0,"locationId":0,"currentConfirmedCountStr":"0"},{"cityName":"麗水",。。。。。。等等等等
'''
總結(jié)
以上是生活随笔為你收集整理的四、正则表达式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。