如何在python中将列添加到现有文本文件中?

时间:2019-06-13 19:33:38

标签: python

我已经在python中创建了一个文本文件,现在我想添加第6列,该列的行长相同,重复以下内容:

red
blue
yellow
green
red
blue
yellow
green
... to the end of the file

我的原始文件是这样的

rtlvis_20190518_13.35.48_00087.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00056.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00117.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00102.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00088.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00043.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491
rtlvis_20190518_13.35.48_00131.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491

我希望它看起来像这样

rtlvis_20190518_13.35.48_00087.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  red
rtlvis_20190518_13.35.48_00056.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  blue
rtlvis_20190518_13.35.48_00117.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  green
rtlvis_20190518_13.35.48_00102.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  yellow
rtlvis_20190518_13.35.48_00088.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  red
rtlvis_20190518_13.35.48_00043.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  green
rtlvis_20190518_13.35.48_00131.bin 29.596454073622454 264.8326389532491 29.596454073622454 264.8326389532491  blue

2 个答案:

答案 0 :(得分:2)

您要寻找的基本概念是模运算符'%'。 https://docs.python.org/3.3/reference/expressions.html#binary-arithmetic-operations

colors = ['red','blue','yellow','green']
with open('file.txt') as f:
    for lineno, line in enumerate(f):
        color = colors[lineno % len(colors)]
        print(line.rstrip() + ' ' + color)

编辑:较大的示例,它写入文件而不是STDOUT:

colors = ['red','blue','yellow','green']
with open('file.txt') as ifh, open('out.txt', 'w') as ofh:
    for lineno, line in enumerate(ifh):
        line = line.rstrip()                 # remove newline
        color = colors[lineno % len(colors)] # choose color
        line += ' ' + color                  # append color
        ofh.write(line + '\n')               # write line

答案 1 :(得分:0)

您应该逐行迭代输入文件,并尝试为其添加适当的颜色。有关更多详细信息,请查看下面的代码段。

colors = ['red', 'blue', 'yellow', 'green']
with open('input.txt') as input_file, open('output.txt', 'w') as output_file:
    for i, line in enumerate(input_file):
        color = colors[i % len(colors)]
        new_line = '{} {}\n'.format(line.rstrip(), color)
        output_file.write(line)

还有另一个解决方案,以提高功能性。让我们检查一下吧!

def get_new_line(t):
    l, c = t
    return '{} {}\n'.format(l.rstrip(), c)

colors = ['red','blue','yellow','green']
with open('input.txt') as input_file, open('output.txt', 'w') as output_file:
    lines = input_file.readlines()
    n, r = divmod(len(lines), len(colors))
    lines_color = colors * n + colors[:r]
    new_lines = list(map(get_new_line, zip(lines, lines_color)))
    output_file.writelines(new_lines)