text - editing a single .txt line in python 3.1 -
i have data stored in .txt file in format:
----------|||||||||||||||||||||||||-----------||||||||||| 1029450386abcdefghijklmnopqrstuvwxy0293847719184756301943 1020414646canbefollowedbyspaces 3292532113435532419963
don't ask...
i have many lines of this, , need way add more digits end of particular line.
i've written code find line want, im stumped how add 11 characters end of it. i've looked around, site has been helpful other issues i've run into, can't seem find need this.
it important line retain position in file, , contents in current order.
using python3.1, how turn this:
1020414646canbefollowedbyspaces 3292532113435532419963
into
1020414646canbefollowedbyspaces 329253211343553241996301846372998
as general principle, there's no shortcut "inserting" new data in middle of text file. need make copy of entire original file in new file, modifying desired line(s) of text on way.
for example:
with open("input.txt") infile: open("output.txt", "w") outfile: s in infile: s = s.rstrip() # remove trailing newline if "target" in s: s += "0123456789" print(s, file=outfile) os.rename("input.txt", "input.txt.original") os.rename("output.txt", "input.txt")
Comments
Post a Comment