【问题标题】:Save the elements of two lists in columns in a file将两个列表的元素保存在文件的列中
【发布时间】:2020-04-28 16:23:35
【问题描述】:

我应该以这种方式将两个列表的元素保存在一个文件中:列表 ID 元素旁边的列表时间戳元素。以下代码将第二个列表的元素放在第一个列表的元素的同一列中。有没有办法更正代码?

    class Videocamera(object):
        timestamp = []
        ID = []
        error = []
        def __init__(self, num_id):
            self.num_id = num_id
    file = open("Rilevazioni videocamera 1.txt", "w")
    V1 = Videocamera("VC00")
    V1.timestamp = [n for n in range(100)]
    for n in V1.timestamp:
        file.write(str(n))
        file.write("\n")
    for n in range(10):
        V1.ID.append(0)
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append("Pe0000")
    for n in range(10):
        V1.ID.append("Pe0001")
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append("Pe0002")
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append(0)
    for n in range(10):
        V1.ID.append("Pe0001")
    for n in range(10):
        V1.ID.append("Pe0000")
    for n in V1.ID:
        if n != 1:
            V1.error.append(False)
        else:
            V1.error.append(True)
    for n in V1.ID:
        file.write(str(n))
        file.write("\n")

【问题讨论】:

    标签: python list file


    【解决方案1】:

    文件编写器按行而不是按列。每次调用“write”时,它都会写入一行。退出循环不会将任何类型的计数器重置为零。

    您应该首先创建您的 V1.ID 列表,然后遍历这两个列表并在行中写入 file.write("{0} {1}".format(timestamp, id))

    您可以将两个列表压缩在一起 (https://docs.python.org/3.8/library/functions.html#zip) 以使迭代更容易。

    既然您回答它不起作用:这不起作用吗?

    class Videocamera(object):
        timestamp = []
        ID = []
        error = []
        def __init__(self, num_id):
            self.num_id = num_id
    file = open("Rilevazioni videocamera 1.txt", "w")
    V1 = Videocamera("VC00")
    for n in range(10):
        V1.ID.append(0)
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append("Pe0000")
    for n in range(10):
        V1.ID.append("Pe0001")
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append("Pe0002")
    for n in range(10):
        V1.ID.append(1)
    for n in range(10):
        V1.ID.append(0)
    for n in range(10):
        V1.ID.append("Pe0001")
    for n in range(10):
        V1.ID.append("Pe0000")
    for n in V1.ID:
        if n != 1:
            V1.error.append(False)
        else:
            V1.error.append(True)
    V1.timestamp = [n for n in range(100)]
    
    for n in range(len(V1.timestamp)):
        file.write(str(V1.timestamp[n])+" "+str(V1.ID[n]))
        file.write("\n")
    

    或,或等价于for n in range(100)。又快又脏,但如果这样就可以了:

    0 0
    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0
    10 1
    11 1
    12 1
    13 1
    14 1
    15 1
    16 1
    17 1
    18 1
    19 1
    20 Pe0000
    21 Pe0000
    22 Pe0000
    23 Pe0000
    24 Pe0000
    

    等等... 是您的预期输出。 如果这不是您的预期输出,您能否澄清一下?

    【讨论】:

    • 它没有按我的意愿工作,但是,谢谢你的回答
    • 对不起,我没有使用你写的代码,有点不同。我的错。
    • 别担心,我的第一个答案没有写清楚。
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多