简体   繁体   中英

How do i add a column to an existing text file in python?

I've created a text file in python, and now I want to add a 6th column with the same length of rows that repeats something like:

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

My original file looks like this

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

and i want it to look like this

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

The basic concept you are looking for is the modulo operator '%'. 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)

EDIT: Larger example that writes to a file instead of 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

You should iterate the input file line by line and try to append appropriate color to it. For more details you can check the below snippet.

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)

Also there's another solution to being more functional. Let's check it!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM