开发者

editing a single .txt line in python 3.1

i have some data stored in a .txt file in this format:

----------|||||||||||||||||||||||||-----------|||||||||||
1029450386abcdefghijklmnopqrstuvwxy0293847719184756301943
1020414646canBeFollowedBySpaces    3292532113435532419963

don't ask...

i have many lines of this, and i need a way to add more digits to the end of a particular line.

i've written code to find the line i want, but im stumped as to how to add 11 characters to the end of it. i've looked around, this site has been helpful with some other issu开发者_Go百科es i've run into, but i can't seem to find what i need for this.

it is important that the line retain its position in the file, and its contents in their current order.

using python3.1, how would you turn this:

1020414646canBeFollowedBySpaces    3292532113435532419963

into

1020414646canBeFollowedBySpaces    329253211343553241996301846372998


As a general principle, there's no shortcut to "inserting" new data in the middle of a text file. You will need to make a copy of the entire original file in a new file, modifying your desired line(s) of text on the way.

For example:

with open("input.txt") as infile:
    with open("output.txt", "w") as outfile:
        for 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")


Check out the fileinput module, it can do sort of "inplace" edits with files. though I believe temporary files are still involved in the internal process.

import fileinput

for line in fileinput.input('input.txt', inplace=1, backup='.orig'):
    if line.startswith('1020414646canBeFollowedBySpaces'):
        line = line.rstrip() + '01846372998' '\n'
    print(line, end='')

The print now prints to the file instead of the console.

You might want to back up your original file before editing.


target_chain = '1020414646canBeFollowedBySpaces    3292532113435532419963'
to_add = '01846372998'

with open('zaza.txt','rb+') as f:
    ch = f.read()
    x = ch.find(target_chain)
    f.seek(x + len(target_chain),0)
    f.write(to_add)
    f.write(ch[x + len(target_chain):])

In this method it's absolutely obligatory to open the file in binary mode 'b' for some reason linked to the treatment of the end of lines by Python (see Universal Newline, enabled by default)

The mode 'r+' is to allow the writing as well as the reading

In this method, what is before the target_chain in the file remains untouched. And what is after the target_chain is shifted ahead. As said by Greg Hewgill, there is no possibility to move apart bits on a hard drisk to insert new bits in the middle.

Evidently, if the file is very big, reading all of its content in ch could be too much memory consuming and the algorithm should then be changed: reading line after line until the line containing the target_chain, and then reading the next line before inserting, and then continuing to do "reading the next line - re-writing on the current line" until the end of the file in order to shift progressively the content from the line concerned with addition.

You see what I mean...


Copy the file, line by line, to another file. When you get to the line that needs extra chars then add them before writing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜