从文件

时间:2016-03-24 14:54:44

标签: python list file tabular

我必须编写一个创建部分魔方桌的程序。 运行我的程序时,表格打印如下:

[int, int, int, int] [int, int, int, int] [int, int, int, int].

我希望它能像我这样打印我的桌子:

[int, int, int, int]
[int, int, int, int]
[int, int, int, int]

我的问题是,如果从文本文件中读取它,我怎么能这样做呢? 我的以下代码尝试是这样的:

text_file = input("Enter the name of the text file: ")
text = open(text_file, "r")
N = int(input("Enter the magic number: "))
print("This is the table")
table = []
nested_list = []
table = text.readlines()


i = 0
for line in table:
    line = line.rstrip()
    line = line.split(" ")
    nested_list = []

    for number in line:
        nested_list.append(int(number))

    table[i] = nested_list
    i += 1
for index in range(1):
    print(table)

1 个答案:

答案 0 :(得分:0)

不确定我是否理解了这个问题,但这不会起作用吗?

for t in table:
    print(t)