久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

瀏覽:128日期: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.baoyu77737.com/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://www.baoyu77737.com/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://www.baoyu77737.com/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.baoyu77737.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)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 泰州市| 木兰县| 二连浩特市| 雷州市| 桦南县| 徐汇区| 天长市| 铜陵市| 石台县| 基隆市| 广汉市| 抚州市| 彰化县| 田阳县| 龙泉市| 镇原县| 山东省| 那坡县| 堆龙德庆县| 武穴市| 轮台县| 奈曼旗| 梅州市| 定日县| 徐水县| 溧水县| 桂林市| 江永县| 景宁| 曲周县| 大同县| 安泽县| 五寨县| 大竹县| 毕节市| 平原县| 武清区| 名山县| 额尔古纳市| 甘泉县| 潜江市|