I have a nested list containing strings and integers that I'm trying to save into a txt file but I'm having trouble with formatting.
array = [(string1, int1),(string2, int2),(string3, int3),...(string_n, int_n)]
with open("output.txt", "w") as f:
f.write(repr(array))
and get the array saved as is.
How do I format the output so that the format is as below instead of the array as is?
string1 int1
string2 int2
.
.
.
string_n int_n
This is propably a very newbie question, but I couldn't find anything similar with search...
Use the following:
array = [('s1', 1),('s2', 2)]
with open('out.txt', 'w') as f:
for item in array:
f.write('{} {}\n'.format(*item))
Output:
s1 1
s2 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With