簡體   English   中英

為什么我的列表中只有一部分寫入輸出文件? (Python)

[英]Why is only a part of my list written to an output file? (Python)

我編寫了一個單熱編碼程序,輸出與在 Linux shell 中顯示時(或使用“>”重定向標准輸出時)完全一樣。 但是,我想實現一些將輸出重定向到新文件的 Python 代碼。 在處理多個列表的特定輸出的 StackOverflow 上,我無法找到此問題的任何答案。

我不確定上下文需要多少代碼,但這是我的輸出示例:

[0, 1, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 1, 0]

這是編譯這些列表的代碼部分:

encoded = list()
for value in integer_encoded:
    base = [0 for x in range(len(bases))]
    base[value] = 1
    encoded.extend(base)
print(encoded)

import sys
sys.stdout = open("output", "w")
print(encoded)
sys.stdout.close()

但是,我的“輸出”文件只包含最后一個列表,我無法弄清楚為什么會發生這種情況。 我真的很感激一些關於如何讓我的整個輸出打印到這個文件的建議。

我不完全明白,但我會盡力提供幫助。

它可能有助於了解integer_encodedbases是什么。

無論哪種方式,寫入文件: with open('output.txt', 'w') as file: file.write(str(encoded))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM