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

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

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

瀏覽:119日期: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://www.lshqa.cn/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://www.lshqa.cn/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://www.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://www.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免费| 狠狠色丁香久久综合网 | 久久久国产精品免费视频 | 97se亚洲综合在线韩国专区福利 | 成人国产精品免费网站 | 国产嫩草影院在线观看 | 草草影院视频 | 一级成人毛片免费观看 | 香港一级纯黄大片 | 一区二区精品在线 | 亚洲一区天堂 | 国产成人久久精品麻豆二区 | 久久99精品久久久久久久野外 | 成人a毛片手机免费播放 | 欧美成人一区二区三区 | 亚洲精品久久久久影 | 日韩欧美在线精品 | 日韩免费一级片 | 韩国一级毛片 | 中文字幕精品一区二区精品 | a一级毛片免费高清在线 | 久久欧美久久欧美精品 | 久久久久久久免费视频 | 日本精高清区一 | 97人摸人人澡人人人超一碰 | 绝对真实偷拍盗摄高清在线视频 | 亚洲性生活视频 | 亚洲精品一区二区手机在线 | 色综合久久久高清综合久久久 | jul-179在线中文字幕 | 伊人婷婷色香五月综合缴激情 | 国产欧美一区二区日本加勒比 | 欧美午夜视频 | 波多野结衣在线观看一区二区 | 国产99视频精品免视看7 | 国产精品黄页在线播放免费 | 亚洲欧美日韩中文字幕在线 | 国产美女又黄又爽又色视频免费 |