如何打印这些元素?

时间:2015-02-21 18:26:28

标签: python python-2.7

class Data:
    def __init__(self, name, place,loc,id):
        self.name = name
        self.place = place
        self.loc=loc
        self.id=id

my_list = []
with open("dat.txt") as f:
   for line in f:
        # line is each line in the file
        # let's pretend our file structure is "NAME PLACE"
        entry = line.strip().split(" ", 5) # data[0] = name, data[1] = place
        my_list.append(Data(entry[0],entry[1],entry[2],entry[3]))

我的文件包含100行,每行有4列。

如何打印所有数据?

1 个答案:

答案 0 :(得分:0)

在我看来,你需要类似的东西

for s in my_list:
    print s.name,s.place,s.id, s.loc

或者那样

for s in my_list:
    print "{}\t{}\t{}\n".format(s.name,s.place,s.loc) # or smth like that