2

I want to write out to values into a file. I get the two values in different if statement. How can I modify it to get the two values within the same output text file?

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)
squaleLis
  • 6,116
  • 2
  • 22
  • 30
krszt
  • 63
  • 1
  • 7
  • One method will be to use a variable to store the tipus values and ertek values like a list or dict and then push the content of the variable to the file. – MEdwin Mar 13 '19 at 09:19
  • You can append to a file by using the mode `a`, which stands for append mode: `open("Outputs.txt", "a")` – Ian Rehwinkel Mar 13 '19 at 09:20
  • Why aren't you using the Hungarian Notation? – Melon Mar 13 '19 at 09:22

2 Answers2

3

This is probably a problem with your open mode w, you should try to use the mode a when opening a file to avoid overwriting,

I link this question as it explains a lot better than I would od python open built-in function: difference between modes a, a+, w, w+, and r+?

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
2

To append the data into a file use a not w. The below code might be helpful

open("Output.txt", "a")
Manoj biroj
  • 288
  • 1
  • 6