gpt4 book ai didi

python - 读取部分文件,以特定单词停止和开始

转载 作者:太空狗 更新时间:2023-10-29 13:48:10 25 4
gpt4 key购买 nike

我正在使用 python 2.7,我被指派(自主分配,我写了这些说明)编写一个小型静态 html 生成器,我想帮助寻找面向 python 的新资源以阅读部分一次的文件。如果有人提供代码答案,那很好,但我想了解 为什么如何 python 工作。我可以买书,但不是很贵的书 - 目前我可以负担得起 30 到 40 美元用于这项特定研究。

这个程序应该工作的方式是有一个 template.html 文件,一个 message.txt 文件,一个 image文件、一个 archive.html 文件和一个 output.html 文件。这比您需要的信息多,但我的基本想法是“来回读取模板和消息,将它们的内容放入输出中,然后写入存在输出的存档”。但我还没有做到这一点,我并不是要你解决整个问题,正如我在下面详述的那样:

程序从 template.html 中读取 html,停在开始标记处,然后从 message.txt 中读取页面标题.那就是我现在的处境。有用!我很高兴……几个小时前,当我意识到那不是最终 Boss 时。

#doctype to title
copyLine = False
for line in template.readlines():
if not '<title>' in line:
copyLine = True
if copyLine:
outputhtml.write(line)
copyLine = False
else:
templateSeek = template.tell()
break

#read name of message
titleOut = message.readline()
print titleOut, " is the title of the new page"
#--------
##5. Put the title from the message file in the head>title tag of the output file
#--------
titleOut = str(titleOut)
titleTag = "<title>"+titleOut+"|Circuit Salsa</title>"
outputhtml.write(titleTag)

我的问题是:我不理解正则表达式,当我尝试各种形式的 for...in 代码时,我得到了所有模板,没有模板,模板部分的某种组合我不想...无论如何,我如何来回阅读这些文件并从我离开的地方继续阅读?非常感谢任何协助寻找更易于理解的资源的帮助,我花了大约五个小时研究这个,但我很头疼,因为我不断获得针对更高​​级受众的资源,但我不理解它们。

这些是我尝试过的最后两种方法(没有成功):

block = ""
found = False
print "0"
for line in template:
if found:
print "1"
block += line
if line.strip() == "<h1>": break
else:
if line.strip() == "</title>":
print "2"
found = True
block = "</title>"

print block + "3"

只打印了点 0 和 3。我把 print # 放在那里是因为我不明白为什么我的输出文件没有改变。

template.seek(templateSeek)
copyLine = False
for line in template.readlines():
if not '<a>' in line:
copyLine = True
if copyLine:
outputhtml.write(line)
copyLine = False
else:
templateSeek = template.tell()
break

对于另一个,我很确定我做错了。

最佳答案

我会使用 BeautifulSoup为了这。另一种方法是使用 regular expressions , 无论如何都很好知道。我知道它们看起来很吓人,但实际上并不难学(我花了一个小时左右)。例如,要获取所有链接标签,您可以执行以下操作

from re import findall, DOTALL

html = '''
<!DOCTYPE html>
<html>

<head>
<title>My awesome web page!</title>
</head>

<body>
<h2>Sites I like</h2>
<ul>
<li><a href="https://www.google.com/">Google</a></li>
<li><a href="https://www.facebook.com">Facebook</a></li>
<li><a href="http://www.amazon.com">Amazon</a></li>
</ul>

<h2>My favorite foods</h2>
<ol>
<li>Pizza</li>
<li>French Fries</li>
</ol>
</body>

</html>
'''

def find_tag(src, tag):
return findall(r'<{0}.*?>.*?</{0}>'.format(tag), src, DOTALL)

print find_tag(html, 'a')
# ['<a href="https://www.google.com/">Google</a>', '<a href="https://www.facebook.com">Facebook</a>', '<a href="http://www.amazon.com">Amazon</a>']
print find_tag(html, 'li')
# ['<li><a href="https://www.google.com/">Google</a></li>', '<li><a href="https://www.facebook.com">Facebook</a></li>', '<li><a href="http://www.amazon.com">Amazon</a></li>', '<li>Pizza</li>', '<li>French Fries</li>']
print find_tag(html, 'body')
# ['<body>\n <h2>Sites I like</h2>\n <ul>\n <li><a href="https://www.google.com/">Google</a></li>\n <li><a href="https://www.facebook.com">Facebook</a></li>\n <li><a href="http://www.amazon.com">Amazon</a></li>\n </ul>\n\n <h2>My favorite foods</h2>\n <ol>\n <li>Pizza</li>\n <li>French Fries</li>\n </ol>\n</body>']

我希望您至少发现其中的一些有用。如果您有任何后续问题,请评论我的回答。祝你好运!

关于python - 读取部分文件,以特定单词停止和开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com