我想把值写到文件中。我在不同的if
语句中得到了两个值。如何修改它以在同一输出文本文件中获取这两个值?
if row == 0 and col == 1:
print ertek
with open("Output.txt", "w") as kiiras:
kiiras.write("data1: %s" % ertek)
kiiras.write("\n")
if type(pattern_colour) is tuple and row == 2:
tipus = thecell.value
tipus = tipus.encode('ascii', 'ignore').decode('ascii')
print tipus
text_file = open("Output.txt", "w")
text_file.write("data2: %s" % tipus)
这可能是你的开放模式w
的一个问题,你应该尝试在打开文件时使用模式a
以避免覆盖,
我把这个问题联系起来,因为它解释了比我python open built-in function: difference between modes a, a+, w, w+, and r+?更好的解释
要将数据附加到文件中,请使用not w。以下代码可能会有所帮助
open("Output.txt", "a")