0

我无法解决如何正确编写这个循环,它已经完成了 90%,但我无法解决这个问题:

string.replace(archive_header, "titlefixed", titlefixed)

必须包含在这个 for 循环中

for arfosers in archive_header, forum_header, search_header, rss_header:
    arfosers.write(rss_header + string.replace(footer_p, "<!-- recent posts -->", postgroup))
    arfosers.close()

(帮助我摆脱“rss header arfosers.write 行)

这些是我想在“for”循环中执行的行:

archive_header = string.replace(archive_header, "titlefixed", titlefixed)
archive_file.write(archive_header + archive_body + string.replace(footer_p, "<!-- recent posts -->", postgroup))
archive_file.close()

forum_header = string.replace(forum_header, "titlefixed", titlefixed)
forum_file.write(forum_header + string.replace(footer_p, "<!-- recent posts -->",     postgroup))
forum_file.close()

search_header = string.replace(search_header, "titlefixed", titlefixed)
search_file.write(search_header + string.replace(footer_p, "<!-- recent posts -->", postgroup))
search_file.close()

rss_header = string.replace(rss_header, "titlefixed", titlefixed)
rss_file.write(rss_header + string.replace(footer_p, "<!-- recent posts -->", postgroup))
rss_file.close()
4

3 回答 3

2

据我所知,每个结构元素都是只读的。尝试使用索引进行循环。

于 2013-08-21T06:25:45.690 回答
1

像这样的东西?

data = (
    (archive_file, archive_header, archive_body),
    (forum_file, forum_header, forum_body),
    (search_file, search_header, search_body),
    (rss_file, rss_header, rss_body)
)

for file, header, body in data:
    header = string.replace(header, "titlefixed", titlefixed)
    file.write(header + body + string.replace(footer_p, "<!-- recent posts -->", postgroup))
    file.close()
于 2013-08-21T06:27:50.950 回答
1
lStuff = [
        (archive_header,archive_file), 
        (forum_header,forum_file), 
        (search_header,search_file), 
        (rss_header,rss_file)
        ]
for header,file in lStuff:
    header = string.replace(header, "titlefixed", titlefixed)
    file.write(header + string.replace(footer_p, "<!-- recent posts -->",postgroup))
    file.close
于 2013-08-21T06:29:29.753 回答