python在执行下面的代码时运行缓慢

问题描述 投票:-1回答:1
tag_api=”htpps://url....”  
json_data=requests.get(tag_api)  
length=len(json_data[‘tags’])  
line=0  
while length>=2:  
    if line<=lenght-1:  
        with open(‘file.txt’,’a’) as fp:  
            fp.write(json_data[‘tags’][line][‘name’])  
            line=line+1  

当我运行此代码时,它在while循环时变慢。你有什么建议吗?

{ { { "id": "", "category": "", "type": "", "tags": { "id": "", "name": "" } { "id": "", "name": "" } }, { "id": "", "category": "", "type": "", "tags": { "id": "", "name": "" } }, { "id": "", "category": "", "type": "", "tags": { "id": "", "name": "" } }, { "id": "", "category": "", "type": "", "tags": { "id": "", "name": "" } { "id": "", "name": "" } }, } }

我试图获取具有两个以上索引的标记并写入文件

linux python-2.7
1个回答
0
投票

打开文件(并关闭它)的操作非常慢。因为每次都是同一个文件,所以你应该尽可能地移开。

© www.soinside.com 2019 - 2024. All rights reserved.