fileinput:for循环结束后无法打印列表

时间:2018-10-17 04:52:18

标签: python python-3.x file-io

我正在尝试为要输入的矩阵创建2D数组,其中每行将 作为数组添加到2D数组 array_1 中。在for循环中,如果键入print,它将进行打印,但是对于最后一行“ print(array_1)” ,则不打印任何内容,也不会给出任何错误消息。是什么原因造成的?我希望能够使用array_1并进行打印。谢谢。

import fileinput
#creates empty array:
array_1 = []
for line in fileinput.input():
#appends the original array:
        new_array = [str(line)]
        array_1.append(new_array)
fileinput.close()
#doesn't print anything here:
print(array_1)

1 个答案:

答案 0 :(得分:-1)

import fileinput
#creates empty array:
array_1 = []
for line in fileinput.input("/home/anil/Downloads/zip_folder_structure"):
    #appends the original array:
    print(line)
    new_array = [str(line)]
    array_1.append(new_array)
fileinput.close()
#doesn't print anything here:
print(array_1)

打印行

['_cluster_frame']
['ca1670d2-f361-450e-be3b-7359711d2199_1']
['image_1.000000_.jpg']

打印array_1

[['_cluster_frame']
['ca1670d2-f361-450e-be3b-7359711d2199_1']
['image_1.000000_.jpg']]

尝试检查您的 for循环语句中的内容。可能您正在获取没有数据的文件。