为什么只将列表的一部分写入输出文件? (蟒蛇)

时间:2019-07-24 09:32:51

标签: 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()

但是,我的“输出”文件仅包含最后一个列表,我无法弄清为什么会这样。我非常感谢您提供一些有关如何使我的整个输出打印到此文件的建议。

1 个答案:

答案 0 :(得分:0)

我不太了解,但我会尽力提供帮助。

这可能有助于了解integer_encodedbases是什么。

以任何一种方式写入文件: with open('output.txt', 'w') as file: file.write(str(encoded))

相关问题