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)
這只給出一行而不是整個結果。結果的型別如下所示。我可以知道如何將整個結果轉換為 txt 檔案嗎?非常感謝。
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
uj5u.com熱心網友回復:
您需要f.write(result)在回圈中呼叫。并在每一行附加一個換行符。
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')
如果您只是要將壓縮元組組合回一個串列,則無需將壓縮元組傳播到變數中。直接使用元組即可。
uj5u.com熱心網友回復:
這一切都是因為結果的值被分配給最后一個值。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/476874.html
