亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

python處理文件內容的正確姿勢該怎樣?

瀏覽:118日期:2022-08-23 11:36:53

問題描述

大神們:

我想把htm文件中的第一個<link到第二個<link之間的所有內容另存為一個htm該怎么寫比較簡潔。

<meta http-equiv='X-UA-Compatible' content='IE=edge'><link rel='prefetch' ><meta name='application-name' content='Python.org'><meta name='msapplication-tooltip' content='The official home of the Python Programming Language'><meta name='apple-mobile-web-app-title' content='Python.org'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='viewport' content='width=device-width, initial-scale=1.0'><meta name='HandheldFriendly' content='True'><meta name='format-detection' content='telephone=no'><meta http-equiv='cleartype' content='on'><meta http-equiv='imagetoolbar' content='false'><script type='text/javascript' async='' src='https://ssl.google-analytics.com/ga.js'></script><script src='http://m.lshqa.cn/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://m.lshqa.cn/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://m.lshqa.cn/wenda/Welcome to Python.org_files/mq.css' rel='stylesheet' type='text/css' media='not print, braille, embossed, speech, tty'>

提取的內容應該是:

<link rel='prefetch' ><meta name='application-name' content='Python.org'><meta name='msapplication-tooltip' content='The official home of the Python Programming Language'><meta name='apple-mobile-web-app-title' content='Python.org'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='viewport' content='width=device-width, initial-scale=1.0'><meta name='HandheldFriendly' content='True'><meta name='format-detection' content='telephone=no'><meta http-equiv='cleartype' content='on'><meta http-equiv='imagetoolbar' content='false'><script type='text/javascript' async='' src='https://ssl.google-analytics.com/ga.js'></script><script src='http://m.lshqa.cn/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link

問題解答

回答1:

import retext = ''with open('read.html', 'r') as rf: text = rf.read() pattern = r'<link[sS]*?<link'results = re.findall(pattern, text)if results: r = results[0] with open('write.html', 'w') as wf:wf.write(r) ================================================with open('read.html', 'r') as rf: with open('write.html', 'w') as wf:num = 0for line in rf.readlines(): if line.startswith('<link'):num += 1continue if num == 2:break wf.writelines(line)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 色综合亚洲七七久久桃花影院 | 国产精品久久久久久久久岛 | 美女免费黄网站 | 萌白酱福利视频 | 亚洲欧美日本综合 | a级片免费 | 欧美一区二区三区免费看 | 精品午夜寂寞黄网站在线 | 亚洲天堂一区二区在线观看 | 精品综合久久久久久98 | 国产l精品国产亚洲区久久 国产tv在线 | 亚洲视频网站在线观看 | 午夜国产 | 免费一级欧美片片线观看 | 免费观看成人www精品视频在线 | 亚洲精彩视频在线观看 | 三级毛片网 | 欧美日韩在线观看一区 | 成年美女黄网站小视频 | 欧美成人毛片 | cao美女视频网站在线观看 | 欧美亚洲在线 | 久久精品2 | 国内精品福利在线视频 | 亚洲男人的天堂网 | 国产午夜精品理论片影院 | 暖暖日本在线播放 | 国产午夜精品久久久久小说 | 国产亚洲精品午夜高清影院 | 国产成人丝袜视频在线视频 | 91香蕉国产观看免费人人 | 一级毛片韩国 | 日韩不卡一级毛片免费 | 久久精品中文字幕首页 | 欧美 日韩 国产在线 | 亚久久伊人精品青青草原2020 | 成人a在线 | 亚洲成人黄色网址 | 精品外国呦系列在线观看 | 欧美激情一区二区亚洲专区 | 暴操美女 |