python處理文件內(nèi)容的正確姿勢(shì)該怎樣?
問(wèn)題描述
大神們:
我想把htm文件中的第一個(gè)<link到第二個(gè)<link之間的所有內(nèi)容另存為一個(gè)htm該怎么寫(xiě)比較簡(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.piao2010.com/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link href='http://www.piao2010.com/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='http://www.piao2010.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.piao2010.com/wenda/Welcome to Python.org_files/modernizr.js.下載'></script><style type='text/css' adt='123'></style><link
問(wèn)題解答
回答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)
相關(guān)文章:
1. python - sqlalchemy更新數(shù)據(jù)報(bào)錯(cuò)2. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?3. javascript - h5微信中怎么禁止橫屏4. macos - 無(wú)法source activate python275. empty比isset更嚴(yán)格一點(diǎn)6. thinkphp6使用驗(yàn)證器 信息如何輸出到前端頁(yè)面7. 我在導(dǎo)入模板資源時(shí)遇到無(wú)法顯示的問(wèn)題,請(qǐng)老師解答下8. PHPExcel表格導(dǎo)入數(shù)據(jù)庫(kù)怎么導(dǎo)入9. javascript - 微信網(wǎng)頁(yè)開(kāi)發(fā)從菜單進(jìn)入頁(yè)面后,按返回鍵沒(méi)有關(guān)閉瀏覽器而是刷新當(dāng)前頁(yè)面,求解決?10. 預(yù)訂金和尾款分別支付

網(wǎng)公網(wǎng)安備