0

所以我有多个格式如下的txt文档:

james  
M  
18  
72   
170  
teresa  
F  
19  
63  
115  

有些只有两个名字,有些有 50 个。基本上,我要做的是格式化信息,以便将其重新写入具有以下格式的新文本文件:

Name: james  
Gender: M  
Age: 18  
Height: 72  
Weight: 170  
Name: teresa  
Gender: F  
Age: 19  
Height: 63  
Weight: 115  

到目前为止,我有:

def tagInfo(fileName):
    with open(fileName) as infile, open("altered.txt","w") as outfile:
        for i,line in enumerate(infile):
            if i == 0:
                outfile.write("Name: "+line.strip()+"\n")
            if i == 1:
                outfile.write("Gender: "+line.strip()+"\n")
            if i == 2:
                outfile.write("Age: "+line.strip()+"\n")
            if i == 3:
                outfile.write("Height: "+line.strip()+"\n")
            if i == 4:
                outfile.write("Weight: "+line.strip()+"\n")
    outfile.close()

这个程序所做的只是编辑前 5 行。我正在努力做到这一点,所以如果我给它一个 50 人的列表来更改,它会更改所有 50 人的信息,而不仅仅是前五行。使用我目前的解决方案,这是不可能的。我不知道从哪里开始,我觉得最好放弃一切并采取不同的方法。

各位有什么解决办法吗?

4

4 回答 4

3

如果您的文件具有固定的字段列表,则将字段定义为列表,并使用带模数的行号作为索引来获取字段名称:

fields = ('Name', 'Gender', 'Age', 'Height', 'Weight')

def tagInfo(fileName):
    with open(fileName) as infile, open("altered.txt","w") as outfile:
        for i, line in enumerate(infile):
            field = fields[i % len(fields)]
            outfile.write('{0}: {1}\n'.format(field, line))

如果超过 5 个,您可以根据需要添加字段。

于 2012-12-07T16:45:39.220 回答
1

将您的检查更改为i % 5 == 0等。这样检查将在每第五行返回一次。

if i % 5 == 0:
    outfile.write("Name: "+line.strip()+"\n")
elif i % 5 == 1:
    outfile.write("Gender: "+line.strip()+"\n")
elif i % 5 == 2:
    outfile.write("Age: "+line.strip()+"\n")
elif i % 5 == 3:
    outfile.write("Height: "+line.strip()+"\n")
elif i % 5 == 4:
    outfile.write("Weight: "+line.strip()+"\n")
于 2012-12-07T16:44:18.343 回答
1

解决方案

修改后的解决方案

只需使用 mod,简短而简单:

def tagInfo(fileName):
    with open(fileName) as infile, open("altered.txt","w") as outfile:
        for i,line in enumerate(infile):
            if i % 5 == 0:
                outfile.write("Name: "+line.strip()+"\n")
            elif i % 5 == 1:
                outfile.write("Gender: "+line.strip()+"\n")
            elif i % 5 == 2:
                outfile.write("Age: "+line.strip()+"\n")
            elif i % 5 == 3:
                outfile.write("Height: "+line.strip()+"\n")
            elif i % 5 == 4:
                outfile.write("Weight: "+line.strip()+"\n")
    outfile.close()


重写的解决方案

这个解决方案会好得多,因为它可以消除代码中的很多欺骗,更不用说让它更干净了:

fields = ("Name", "Gender", "Age", "Height", "Weight") # can be edited as per req.s
l = len(fields)

def tagInfo(fileName):
    with open(fileName) as infile, open("altered.txt","w") as outfile:
        for (index, s) in enumerate(infile):
             field = fields[index % l]
             outfile.write("{}: {}\n".format(field, s.strip())
    outfile.close()


模组功能

文档

%(取模)运算符产生第一个参数除以第二个参数的余数。数字参数首先转换为通用类型。零右参数引发 ZeroDivisionError 异常。参数可以是浮点数,例如,3.14%0.7 等于 0.34(因为 3.14 等于 4*0.7 + 0.34。)模运算符总是产生与第二个操作数(或零)具有相同符号的结果。结果的绝对值严格小于第二个操作数的绝对值。


TL;博士:

模函数(也称为 mod 函数)基本上只是给出除第一个数字的余数。到第二个。所以a % b = cmp(b, 0) * abs(a - (a // b))(我认为)。

于 2012-12-07T16:44:38.873 回答
0

只是另一种解决问题的方法

fields = ('Name', 'Gender', 'Age', 'Height', 'Weight')
with open("infile.txt") as fin, open("altered.txt","w") as fout:
    while True:
        data =  ''.join(map(':'.join,zip(fields,fin)))
        if data:
            fout.write(data)
        else:
            break
于 2012-12-07T16:58:20.613 回答