国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python處理文件內(nèi)容的正確姿勢(shì)該怎樣?

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

問題描述

大神們:

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

<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.intensediesel.com/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://www.intensediesel.com/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://www.intensediesel.com/wenda/Welcome to Python.org_files/mq.css' rel='stylesheet' type='text/css' media='not print, braille, embossed, speech, tty'>

提取的內(nèi)容應(yīng)該是:

<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.intensediesel.com/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)

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 岚皋县| 内丘县| 泊头市| 乌兰浩特市| 六盘水市| 上饶县| 陆丰市| 玉环县| 和平区| 扬中市| 鄂托克旗| 无极县| 罗甸县| 常熟市| 龙川县| 嘉鱼县| 枣强县| 汽车| 光山县| 北安市| 蒲江县| 肥乡县| 铜山县| 双鸭山市| 江山市| 秦皇岛市| 泰兴市| 澄迈县| 罗城| 荆门市| 措美县| 资兴市| 四会市| 濉溪县| 张北县| 石阡县| 盐山县| 广丰县| 海南省| 临邑县| 嘉善县|