Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7 - Fatal编程技术网

Python 写入文件而不读取所有文件

Python 写入文件而不读取所有文件,python,python-2.7,Python,Python 2.7,我有一个带有页眉和页脚的文件模板(html)。我尝试在之后插入文本。 我现在的做法是使用fileinput.input() 我应该把它变成: block_of_lines='' for json in jsonfolder : Object_a = CLASS-A(json) #unserialization block_of_line += Object_a.to_html_sting() Create_html(block_of_line) 那会更快吗?5000行算不了什么

我有一个带有页眉和页脚的文件模板(html)。我尝试在
之后插入文本。 我现在的做法是使用
fileinput.input()

我应该把它变成:

block_of_lines=''
for json in jsonfolder :
    Object_a = CLASS-A(json) #unserialization
    block_of_line += Object_a.to_html_sting()
Create_html(block_of_line)

那会更快吗?

5000行算不了什么。使用
f.readlines()
读取整个文件以获取行列表:

with open(path) as f:
    lines = f.readlines()

然后处理每一行,最后将它们连接到一个字符串,并将整个内容写回文件。

将问题再读几遍,出现以下想法。 你能把书写分成3块吗?一块是页眉,一块是表格行,另一块是页脚。这似乎取决于这三个替换行在做什么,但如果我是对的,它们只能在第一次使用模板时更新行,即在处理第一个json文件时,然后对其他行保持不变

 file_footer = CLASS-A.write_html_header(path)
 for json in jsonfolder :
     Object_a = CLASS-A(json) #unserialization
     Object_a.write_to_html(path)   #use the part of the function
                                # that just handles the json file here
 CLASS-A.write_html_footer(path, footer)
然后在类中,定义两个新函数,将页眉和页脚作为静态方法写入(这意味着它们可以从类中使用,而不仅仅是在实例中使用) i、 e.(使用您自己代码的副本)


这样,在json文件上循环时,您就不会重新读取您创建的文件位。如果这是一个问题,您也不会试图将整个文件存储在内存中。

如果我理解正确,此文件中的某个地方有一个
,您可以运行上述代码5000次,向其中添加5000个不同的
行?为什么不把所有5000行都放在一个列表中,一次将它们全部添加到
if''in line:
块中呢?您能在示例中有注释“####PRINT MY stuff”的地方打个电话,在该点打印所有5k行,然后只调用示例代码一次,而不是5k次吗?另外,这是否意味着您调用了以相反顺序传递表格行的示例代码,因为每一行都是在表行之后插入的,即在前一行的正上方插入的,这似乎是一项艰巨的工作。您是否尝试过保存到另一个文件,而不是原地写入?@R.Sharp我确实将它们以相反的顺序插入。但是顺序并不重要,js脚本在显示时会对表进行排序。我尝试只调用一个call5000表行--->每个表行是30个html代码行,所以在最终文件中是150k行。但每次我添加一个表格行时,我都会在中读取30 X表格行的编号。最后读取的行总数是30(1+2+3+…+5000),我认为您应该尝试全部读取。你会失去什么?如果你这样做了,请添加一条评论…这就是我现在正在做的,我最终阅读了375KK行(输出大约需要10分钟),伙计们,好吧,我相信OP知道我的答案不是正确的,有评论,它告诉我们,尽管这可能仍然是合理的,但这里的情况并非如此,而且不需要保持不通知:-)我有一个函数,可以执行
copy(template.html,path)
然后我只处理副本。这是一个很好的方法。我会努力的
with open(path) as f:
    lines = f.readlines()
 file_footer = CLASS-A.write_html_header(path)
 for json in jsonfolder :
     Object_a = CLASS-A(json) #unserialization
     Object_a.write_to_html(path)   #use the part of the function
                                # that just handles the json file here
 CLASS-A.write_html_footer(path, footer)
@staticmethod
def write_html_header(path):
    footer = []
    save_for_later = false
    for line in fileinput.input(path, inplace=1):
        line = re.sub(r'CURRENT_APPLICATION', obj, line)
        line = re.sub(r'IN_PROGRESS', time.strftime("%Y-%m-%d %H:%M:%S"), line)
        line = re.sub(r'CURRENT_VERSION', svers, line)
        # this blocks prints the header, and saves the
        # footer from your template.
        if save_for_later:
            footer.append(line)
        else:
            print line,  # preserve old content
        if "<tbody>" in line:
            save_for_later = true
    return footer
open the template (read only)
open the new_file (in new, write mode)
write the header into new_file
loop over json files
    append table content into new_file
append the footer into new_file