Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在满足特定条件的特定行之后,将整个文件写入另一个文件_Python_Html_File_Text_Insert - Fatal编程技术网

Python:在满足特定条件的特定行之后,将整个文件写入另一个文件

Python:在满足特定条件的特定行之后,将整个文件写入另一个文件,python,html,file,text,insert,Python,Html,File,Text,Insert,我正在编写一个Python脚本,当我运行它时,它将把来自不同文件的文本拼凑在一起,这样我就可以创建一个网站的替代版本,方便地比较不同的设计,并确保它们都具有相同的固有数据,即。确保所有版本的菜单项一致 一个具体的问题是确保菜单(不同食物的聚宝盆)总是相同的。所以我做了这个函数: def insert_menu(): with open("index.html", "r+") as index: with open("menu.html", "r+") as menu:

我正在编写一个Python脚本,当我运行它时,它将把来自不同文件的文本拼凑在一起,这样我就可以创建一个网站的替代版本,方便地比较不同的设计,并确保它们都具有相同的固有数据,即。确保所有版本的菜单项一致

一个具体的问题是确保菜单(不同食物的聚宝盆)总是相同的。所以我做了这个函数:

def insert_menu():
    with open("index.html", "r+") as index:
        with open("menu.html", "r+") as menu:
            for i in index:
                if "<!-- Insert Menu here -->" in i:
                    for j in menu:
                        index.write(j)
请注意,在
index.html
中,上面的块都缩进到一个较大的div中,因此我无法在这里轻松复制它

如何更改代码以达到预期的结果,或者我是以一种非常迂回的方式来实现这一点的


还有,我怎样才能澄清这个问题来帮助你帮助我呢?

你能试试这个并告诉我它是否有效吗?因为我没有你的文件,假设你已经读过了,并且它是以字符串的形式存储的

string = "blablabla \n  blebleble \n <!-- Insert Menu here --> \n bliblibli"
pattern = ".*<!-- Insert Menu here -->(.*)"
print re.match(pattern, string, re.S).groups()
string=“blabla\n bleble\n\n blibli”
pattern=“*(*)”
打印re.match(模式、字符串、re.S).groups()
这将匹配插入菜单-->之后的任何内容,包括任何空格和换行。如果要跳到下一行:

pattern = ".*<!-- Insert Menu here --> .*\n (.*)"
pattern=“.*.*.\n(.*)”
更新:刚刚意识到这意味着读取整个文件,这可以接受吗?
干杯

假设文件不是很大,一个简单的字符串替换就可以了:

def insert_menu():
    with open("index.html") as index:
        index_text = index.read()
    with open("menu.html") as menu:
        menu_text = menu.read()
    # I called it index2 so it doesn't overwrite... you can change that
    with open("index2.html", "w") as index2:
        index2.write(index_text.replace('<!-- Insert Menu here -->', menu_text))
def插入菜单():
以open(“index.html”)作为索引:
index_text=index.read()
打开(“menu.html”)作为菜单:
menu_text=menu.read()
#我把它叫做index2所以它不会覆盖。。。你可以改变
以open(“index2.html”、“w”)作为index2:
index2.write(索引文本替换(“”,菜单文本))

既然另一个似乎不起作用,那么这个选项呢(无可否认,不那么优雅):

条件=False
new_string=“”
将open(“index.html”)作为f:
对于f中的行:
如果条件==真:
新建字符串=新建字符串+行+“\n”
如果“”在第行中:
条件=真
然后您可以将新字符串打印到新文件中


这行吗?

尝试就地更新源文件将不起作用。在阅读时写入“索引”不会做你想做的事情-它会覆盖你引人注目的字符串后面的行

相反,您应该将“索引”和“菜单”源文件都视为输入,并创建第三个文件作为输出(基本上是将两个输入文件合并为一个组合输出文件)。使用“经典”语法:

output = open('merged.html', 'w')
for line in open('index.html'):
    if '<!-- Insert Menu here -->' in line:
        for menuline in open('menu.html'):
            output.write(menuline)
    else:
        output.write(line)
output=open('merged.html','w')
对于打开的行('index.html'):
如果“”在第行中:
对于打开的菜单行('menu.html'):
output.write(菜单行)
其他:
输出。写入(行)

如果您愿意的话,可以将其改为“使用”语法。

可能会对您有所帮助。我之前遇到过这一点,我目前正试图用它作为指导来写一些东西,但是我还没有成功。你可能想把
索引中的行
菜单中的行
重命名为类似于
索引中的I和菜单中的ix的东西。因为你没有读行。请发布更多代码。通常,您希望使用来生成HTML文档,而不是标准库的
read
write
方法。@Drogans修改了命名。鉴于函数是独立的,您希望发布什么代码?现在正在查看BeautifulSoup。它提供的远远超过了这个项目的要求,因此我还不太热衷于实施它。遗憾的是,这没有起作用。尽管我尽了最大的努力,但还是出现了这个错误:
print re.match(pattern,string,re.S).groups()
AttributeError:“NoneType”对象没有属性“groups”
我的代码,我的字符串,它对你有用吗?如果是这样的话,请检查您的文件,并查看代码中显示的内容。你发布的代码运行良好。适用于我的情况,它没有。可能是因为图案缩进了。我已经在上面提供了一个原始副本。您需要更改代码以完全匹配您自己代码中的内容。。。如果文本在那里,它必须工作。。。嗯,你能复制并粘贴“此处插入菜单”+/-1的行,看看问题出在哪里吗。通过我的问题检查以上内容。正如上面提到的,如果不提供太多代码,我无法充分复制文件中的缩进,这只会让人感到困惑。如果我现在编辑的上述示例不足以让我知道,我将按原样提供缩进。
condition = False
new_string = ""
with open("index.html") as f:
    for lines in f:
        if condition == True:
            new_string = new_string + lines + "\n"
        if "<!-- Insert Menu here -->" in lines:
            condition = True
output = open('merged.html', 'w')
for line in open('index.html'):
    if '<!-- Insert Menu here -->' in line:
        for menuline in open('menu.html'):
            output.write(menuline)
    else:
        output.write(line)