0

到目前为止,这是我所拥有的,

def List():
    bad_filename = True
    while bad_filename == True:
        try:
            filename = input("Enter the filename: ")
            fp = open(filename, "r")
            for f_line in fp:
                b=(f_line.strip('\n').split(','))
                l=[float(v) for v in b]
                l=[]
                for v in b:
                    l.append(float(v))
                print (l,end=" ")

            bad_filename = False
        except IOError:
            print("Error: The file was not found: ", filename)

def tab():
    A=()
    print_str = "|{:^5s} | {:^10s} | {:^10s}| {:^10s} |{:^10s}|".format("Name","A", "B","C","D","E")
    hline = "-" * len(print_str)
    lline="-"*len(print_str)
    print(hline)
    print(print_str)
    print(lline)
    format_str= "|{:>5s} |{:^10.2f}  |{:^10.2f} |{:^10.2f}  |{:>10f}|"
    print_str= format_str.format('1',1.00,2.00,3.00,4.00,5.00)
    print (print_str)
    print_str=format_str.format('2',6.00,7.00,8.00,9.00,0.00)
    print(print_str)
    print_str=format_str.format('3',1.1,2.2,3.3,0.1,0.3)
    print(print_str)

List()
tab()

当用户更改 .txtfile 中的数据时,我的问题就开始了,因为我将数字硬编码到打印表中,并且我还需要创建一个数组,以便从列表中获取最小最大值。

而不是像这样的硬代码值 ('1',1.00,2.00,3.00,4.00,5.00) 我希望它从 .txt 文件中读取并在 txt 文件中生成表我已将数据存储在桌面 1,2 中, 3,4,5 6,7,8,9,0 1.10,2.20,3.30,0.10,0.30

4

1 回答 1

0

有些东西可以尝试...

首先,重新格式化您的List()函数以摆脱额外的代码并返回一个列表,而不是打印它,这样您就可以处理它......

def GenList():
    bad_filename = True
    while bad_filename == True:
        try:
            filename = input("Enter the filename: ")
            fp = open(filename, "r")
            l = []

            for f_line in fp:
                rownum += 1
                b = f_line.strip('\n').split(',')
                l.append([float(v) for v in b])
            fp.close()
            bad_filename = False
            return l    
        except IOError:
            print("Error: The file was not found: ", filename)

现在调用该函数来获取您的列表:

MyList = GenList()

对于您的其他打印功能,使其将“参数”作为输入(此列表)并在您的后续.format()语句中使用它:

def MakeTable(InputList):
    format_str= "|{:>5s} |{:^10.2f}  |{:^10.2f} |{:^10.2f}  |{:>10f}|"
    print("The max of the list is {0} and the min is {1}".format(max(InputList),min(InputList)) )
    # etc....
于 2013-10-29T01:58:24.783 回答