【发布时间】:2019-05-04 13:20:39
【问题描述】:
我有一个 with 循环,我在其中打开两个 json 文件并在我的循环中进行特定比较。我有各种条件语句,根据满足哪个条件,我想将结果输出到文件中。现在我不确定它在 for 循环中的位置。我目前在我的循环中有打印语句,它连接字符串和变量。我想将其替换为写入文件。
with open('file1.json', 'r') as f, open('file2.json', 'r') as g:
for cd, pd in zip(f, g):
if condition:
if condition:
print "I would like to output this to a file":
else:
print "I would like to output this to a file"
else: # file names do not match
print "I would like to output this , str(variable)"
【问题讨论】:
-
我删除了
python-3.x标签,因为您使用的是打印语句,这意味着您不能使用Python 3。请不要标记不相关的版本。 -
没问题@khelwood
-
那么问题出在哪里?为什么不能直接写入文件而不是使用
print? -
h.write("This is being written to a file\n") -
您需要使用串联:
h.write("File" + str(current_fn) + "with file size" ...)。或者使用格式化运算符使其更易于阅读。
标签: python python-2.7 file while-loop