python处理文件内容的正确姿势该怎样?
问题描述
大神们:
我想把htm文件中的第一个<link到第二个<link之间的所有内容另存为一个htm该怎么写比较简洁。
<meta http-equiv='X-UA-Compatible' content='IE=edge'><link rel='prefetch' href='https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'><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='https://www.haobala.com/wenda/Welcome to Python.org_files/modernizr.js.下载'></script><style type='text/css' adt='123'></style><link href='https://www.haobala.com/wenda/Welcome to Python.org_files/style.css' rel='stylesheet' type='text/css'><link href='https://www.haobala.com/wenda/Welcome to Python.org_files/mq.css' rel='stylesheet' type='text/css' media='not print, braille, embossed, speech, tty'>
提取的内容应该是:
<link rel='prefetch' href='https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'><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='https://www.haobala.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)
相关文章:
1. docker内创建jenkins访问另一个容器下的服务器问题2. Whitelabel错误页面发生意外错误(类型=未找到,状态= 404)/WEB-INF/views/home.jsp3. html5 - h5写的app用的webview,用手机浏览器打开不显示?4. docker-compose 为何找不到配置文件?5. 用Java8的 stream 操作外部集合是否存在并发问题?6. docker容器呢SSH为什么连不通呢?7. docker gitlab 如何git clone?8. html5和Flash对抗是什么情况?9. css3 - 请问,如何写这个颜色的(渐变),并且在移动端自适应10. javascript - 移动端,当出现遮罩层的时候,遮罩层里有div是超出高度scroll的,怎么避免滑动div的时候,body跟随滑动?
