先看两段代码:
1、
-
def input_data_wow():
-
channels_massage = get_db_neteasewow()
-
if os.path.exists("wow_list_dong")==True:
-
os.remove("wow_list_dong")
-
for a in channels_massage:
-
b = a[1] + " " + a[2] + " " + a[3]
-
with open("wow_list_dong","ab") as f:
-
b += "\n"
-
f.writelines(b)
2、
-
def input_data_wow():
-
channels_massage = get_db_neteasewow()
-
with open("wow_list_dong","w") as f:
-
for a in channels_massage:
-
b = a[1] + " " + a[2] + " " + a[3]
-
b += "\n"
-
f.write(b)
上面两段代码的实现功能是一样的;然而第一段代码的执行效率明显低于第二段代码;
因为第一段代码每次都要打开文件,然后写入,而第二段代码,是先打开文件然后进行循环输入,这样还能避免写入文件时被覆盖;
阅读(21864) | 评论(0) | 转发(0) |