【问题标题】:How to match chunks of textfile-data-lines to categories before printing?如何在打印之前将文本文件数据行块与类别匹配?
【发布时间】:2016-04-15 01:10:39
【问题描述】:

我有一个 txt 文件,其中包含 x 个不同属性列表的数据。在我的 program.py 中,txt 文件中的每一行都通过 readlines() 排序到一个列表中,即每个列表元素都包含 txt 文件中的一行数据,我们将列表称为“属性”。对于 txt 文件中的每个属性,例如有 4 行数据:


在文本文件中:

900000

200

50

街道名称1

80000

100

30

街道名称2

如果 90000(txt 文件中的第一行)是房产一的价格,200 是租金,50 代表平方米,然后是街道名称,那么有一个空行,然后是房产二的新数据,价格为 80000 等.

我想将列表“properties[]”中的数据元素(每个元素是文本文件中的一行)打印给用户,但同时通过程序为每个数据行添加类别(而不是来自文本文件) ,然后对所有属性执行此操作,以便打印出所有属性的数据。每个属性的类别都相同,因此它们与 txt 文件中每个块的数据行匹配。这是一个关于我如何为一个属性编写它的示例:

print("\n"+"Price: "+properties[0]
      +"Rent: "+properties[1]
      +"Size: "+properties[2]
      +"Street: "+properties[3])

如何创建一个遍历所有元素、列表“属性”中的所有数据并将它们打印到正确类别的公式?

我希望输出是:

价格:900000

租金:200

尺寸:50

街道:街道名称 1

空行

价格:800000

租金:100

尺寸:30

街道:Steetname2

...然后计划为每个街区、每个属性制作一侧

【问题讨论】:

  • 请提供您想要的输出方式

标签: python list loops printing text-files


【解决方案1】:
lines = open("file.txt", 'r').readlines()
properties = zip(*[x[i::4] for i in range(4)])
param_names = ("Price", "Rent", "Size", "Street")
for property in properties:
    print('\n')
    print('\n'.join(["%s: %s" % k, v for k, v in zip(param_names, property)]))

【讨论】:

    【解决方案2】:

    我会为此使用一个矩阵,然后循环遍历该矩阵。对于您提供的输入,我会做这样的事情

    
    
        #Open text file
        with open("example.txt","r") as textFile:
            #Read file and break into array of strings
            text = textFile.read().split()
        #Create empty arrays to store the formated text
        matrixOfProperties = []
        properties = []
        #Loop thru strings to format into an array of arrays of properties
        for index in  range(0,len(text)):
            #Apend propertie to the array of properties
            properties.append(text[index])
            '''
            When all the 4 properties where appended to the
            array of properties, appends the array to the
            matrix and clear the array
            '''
            if index%4 == 3:
                matrixOfProperties.append(properties)
                properties = []
        #Loops over the matrix printing all the elements
        for propertie in matrixOfProperties:
            print("\nPrice: "+propertie[0]
              +"\nRent: "+propertie[1]
              +"\nSize: "+propertie[2]
              +"\nStreet: "+propertie[3])
    
    
    

    输入: 900000

    200

    50

    街道名称1

    80000

    100

    30

    街道名称2

    输出:

    价格:900000
    租金:200
    尺寸:50
    街道:Streetname1

    价格:80000
    租金:100
    尺寸:30
    街道:Streetname2

    【讨论】:

    • 谢谢!如果例如 Street: Streetname 2 ,即 streetname 和 2.. 之间的空格,我如何使它工作,然后我得到两个字符串而不是列表中的一个..?
    • 嗯,你必须在打印之前编辑你的字符串,这取决于 txt 文件的设置方式,可能会有点困难......如果它在你的 txt 文件中设置为 Streetname2 并且你想要打印为 Streetname 2 使用 for 继续该字符串并添加空格。如果它在您的文本文件中作为 Streetname 2 出现,那么我将消除输入行之间的空格并使用 .split('\n') 而不是 .split()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    相关资源
    最近更新 更多