如何使for-loop写特定行

时间:2013-12-11 18:38:45

标签: python for-loop text-files

我有一个包含一组数据的文本文件。写入新文件时,我正在使用for循环,但我的for循环为每行写入每一行。例如:我的数据是

Hello
World
This
Is
The
Text

我的预期输出是使用xml标签使用字符串标记数据。输出应该类似于

<first>Hello</first>
<second>World</second>
<third>This</third>
""
""
<sixth>Text</sixth>

等等。然而,循环我使用我的输出看起来像

<first>Hello</first>
<second>Hello</second>
<third>Hello</third>
<fourth>Hello</fourth>
<fifth>Hello</fifth>
<sixth>Hello</sixth>
<first>World</first>
<second>World</second>
<third>World</third>
<fourth>World</fourth>
<fifth>World</fifth>
<sixth>World</sixth>
""
""

等等直到它结束。我该如何解决。我的代码是

def tagData(filename):
    original = open(filename, 'r')
    new = open('test2.txt', 'w')
    index = 0
    for line in original:
        if index%6 == 0:
            new.write('<first>'+str(line).strip('\n')+'</first>\n')
            index = index + 1
        if index%6 == 1:
            new.write('<second>'+str(line).strip('\n')+'</second>\n')
            index = index + 1
        if index%6 == 2:
            new.write('<third>'+str(line).strip('\n')+'</third>\n')
            index = index + 1
        if index%6 == 3:
            new.write('<fourth>'+str(line).strip('\n')+'</fourth>\n')
            index = index + 1
        if index%6 == 4:
            new.write('<fifth>'+str(line).strip('\n')+'</fifth>\n')
            index = index + 1
        if index%6 == 5:
            new.write('<sixth>'+str(line).strip('\n')+'</sixth>\n')
            index = index + 1
    original.close()
    new.close()

3 个答案:

答案 0 :(得分:7)

我写的是:

from itertools import cycle, izip

tags = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
with open('input') as fin, open('output', 'w') as fout:
    for tag, line in izip(cycle(tags), fin):
        fout.write('<{0}>{1}</{0}>\n'.format(tag, line.strip()))

这可以避免使用索引和if逻辑......

答案 1 :(得分:4)

您在每个if语句中递增索引。

试试这个:

def tagData(filename):
    original = open(filename, 'r')
    new = open('test2.txt', 'w')
    index = 0
    for line in original:
        if index%6 == 0:
            new.write('<first>'+str(line).strip('\n')+'</first>\n')
        elif index%6 == 1:
            new.write('<second>'+str(line).strip('\n')+'</second>\n')
        elif index%6 == 2:
            new.write('<third>'+str(line).strip('\n')+'</third>\n')
        elif index%6 == 3:
            new.write('<fourth>'+str(line).strip('\n')+'</fourth>\n')
        elif index%6 == 4:
            new.write('<fifth>'+str(line).strip('\n')+'</fifth>\n')
        elif index%6 == 5:
            new.write('<sixth>'+str(line).strip('\n')+'</sixth>\n')
        index = index + 1
    original.close()
    new.close()

答案 2 :(得分:2)

保证index正确的更好方法是使用enumerate。这就是它的用途:

def tagData(filename):
    original = open(filename, 'r')
    new = open('test2.txt', 'w')
    for index, line in enumerate(original):
        # etc.

现在你不必在任何地方写index += 1,这意味着你不可能弄错,所以你的问题一开始就不会出现。


虽然我们正在努力,但还有其他一些方法可以简化这一过程。

  • 虽然if / elif链比一堆独立的if更好,但使用dict将索引映射到字符串会更好。< / LI>
  • 如果您使用with语句,则不需要这些明确的close来电。
  • 当您使用format方法或%运算符而不是字符串连接时,读取和编写复杂的字符串格式会容易得多。
  • 由于line已经是字符串,因此不需要str(line)

所以:

tagnames = {0: 'first', 1: 'second', 2: 'third', 
            3: 'fourth', 4: 'fifth', 5: 'sixth'}

def tagData(filename):
    with open(filename, 'r') as original, open('test2.txt', 'w') as new:
        for index, line in enumerate(original):
            tagname = tagnames[index%6]
            new.write('<{}>{}</{}>\n'.format(tagname, line.strip('\n'), tagname))

但请注意,在这种情况下,dict键只是从0开始的连续数字。在许多程序中都不是这样,但它就在这里,所以让我们利用它,并使用一个list

tagnames = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
# same code as above