for (a, b, c, d, e, f, g) in zip(A, B, C, D, E, F, G):
result = ''.join([a,b,c,d,e,f,g])
# Write the file
with open("file.txt","w") as f:
f.write(result)
This only gives one line instead of the whole result. The type of the result is shown below. May I know how to convert the whole result to a txt file? Thanks a lot.
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
CodePudding user response:
You need to call f.write(result)
in the loop. And append a newline to each line.
with open("file.txt","w") as f:
for data in zip(A, B, C, D, E, F, G):
result = ''.join(data)
f.write(result '\n')
There's no need to spread the zipped tuples into variables if you're just going to combine them back into a list. Just use the tuples directly.
CodePudding user response:
This is all happens because the value of result is assign to the last value.
result = []
for (a, b, c, d, e, f, g) in zip(A, B, C, D, E, F, G):
result.append(''.join([a,b,c,d,e,f,g]) '\n')
# Write the file
with open("file.txt","w") as f:
f.writelines(result)