无法解决for循环问题

时间:2013-08-21 06:18:28

标签: python

我无法解决如何正确编写此循环,已完成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 line)

这些是我想在'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()

3 个答案:

答案 0 :(得分:2)

每个结构元素只读我知道。尝试使用索引进行循环。

答案 1 :(得分: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()

答案 2 :(得分: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