当前位置: 代码迷 >> python >> 创建记录表(数组)
  详细解决方案

创建记录表(数组)

热度:45   发布时间:2023-07-14 09:52:55.0

如果我想将两个文件中的记录存储到一个表(一个记录数组)中,可以使用类似于以下代码的格式,然后将两个文件名都放在def函数中,例如def readTable(log1,log2):为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

只需使用* 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(任意数量的日志,并获取每个日志的表列表)。

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

readTable(log1) + readTable(log2)
  相关解决方案