我正在阅读 StackOverflow 中的一个问题,其中用户连续两次应用“with”语句,将结果从第一个 with 语句中声明的变量流水线化到第二个。像这样(简单的例子):
with open('first_file.txt', 'r') as f:
loaded_file = f.readlines()
#...Prepare a csv file somehow - loaded_file is not declared outside with...
with open("second_file.csv", "w") as f:
for line in loaded_file:
f.write(line+"\n")
考虑到变量作用域,为什么它会起作用?
慕运维8079593
30秒到达战场
相关分类