午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

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

瀏覽:194日期: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.leifengta.com.cn/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://www.leifengta.com.cn/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://www.leifengta.com.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.leifengta.com.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 編程
相關文章:
主站蜘蛛池模板: 国产91精品一区二区绿帽 | 人人艹超碰| 成人一级片在线观看 | 成人av福利| 日韩精品小视频 | 毛片在线看片 | 亚洲字幕在线观看 | 永久免费精品视频 | 中文字幕1区 | 日韩毛片网| 99av国产精品欲麻豆 | av在线大全 | 午夜合集 | 亚洲色图另类小说 | 日韩六区 | 日韩三级在线免费观看 | 国产在线观看成人 | 精品一区二区三区中文字幕 | 国产精品播放 | 对白超刺激精彩粗话av | 五月综合久久 | 精品久久成人 | 69婷婷国产精品入口 | 天堂网中文字幕 | 亚洲vs天堂| 精品久久久久久久久久久久 | 日韩在线天堂 | 久久久久在线观看 | 免费在线看黄的网站 | 日日操日日 | 亚洲精品福利在线观看 | 久久福利片| 朝桐光av一区二区三区 | 欧美特大黄 | 成人在线一区二区 | 欧美成人一二三区 | 国产91在线看 | 午夜青青草 | 久久久一级 | 国产成人精品一区二区三区四区 | 中文字幕免费观看视频 |