创建记录表(数组)

2024-09-30 02:27:47 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我想将两个文件中的记录存储到一个表(一个记录数组)中,是否可以使用类似于下面代码的格式,将两个文件名都放入def readTable(log1,log2)这样的def函数中:然后对log1和log2使用相同的代码,这样就可以生成table1和table2?你知道吗

def readTable(fileName):
    s = Scanner(fileName)
    table = []
    record = readRecord(s)
    while (record != ""):
        table.append(record)
        record = readRecord(s)
    s.close()            
    return table

Tags: 文件函数代码文件名def格式记录table
2条回答

由于readTable返回一个列表,因此如果要连接两个日志中的记录,请使用+运算符。你知道吗

readTable(log1) + readTable(log2)

只需使用*args,并获取记录列表?你知道吗

def readTable(*args):
    tables = []
    for filename in args:
        s = Scanner(fileName)
        table = []
        record = readRecord(s)
        while (record != ""):
            table.append(record)
            record = readRecord(s)
        s.close()
        tables.append(table)
    return tables

这样,您就可以传递log1、log2、log3(任意数量的日志),并为每个日志返回一个表列表

相关问题 更多 >

    热门问题