#1
  1. No Profile Picture
    tom_cos
    Guest
    Devshed Newbie (0 - 499 posts)

    Search Replace Image Numbers


    I'm nearly totally unknowledgable of Python; but here is the scenario I am trying to solve. I have a txt file with several thousand lines of text in it. About 200 of the lines say "-----File: 001.png-----". I'd like to renumber (all but the first one) by incrementing each one (002.png, 003.png, etc.).

    How can I accomplish this with a Python script?

    The general flow would be something along the lines of:

    Open "mytextfile.txt"
    While (NOT EoF):
    nmbr=2
    Search: "-----File: %d.png-----"
    Replace: -----File: %(number)03d.png-----
    nmbr = nmbr + 1
    Save "mytextfile.txt"

    Any help is appreciated!

    Thanks, Tom C.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Normally I'd use gawk for this sort of program. python's not too bad though.
    Code:
    '''
    original mytextfile.txt content:
    -----File: 001.png-----
    -----File: 0771.png-----
    -----File: not_a_number.png-----
    
    final mytextfile.txt content:
    -----File: 001.png-----
    -----File: 002.png-----
    -----File: 003.png-----
    '''
    
    
    import os
    original = 'mytextfile.txt'
    backup = original+'.bak'
    os.rename(original,backup)           # preserve a copy of the original
    with open(backup,'r') as inf:
        with open(original,'w') as ouf:
            n = 1                 # I assume the first match is numbered 1
            for line in inf:
                fields = line.split()
                if '-----File:' == fields[0]:
                    of = fields[1].split('.')
                    ouf.write('%s %03d.%s\n'%(fields[0],n,of[1]))
                    n += 1
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    tom_cos
    Guest
    Devshed Newbie (0 - 499 posts)
    My in file is a bit more complex. The "-----File:" lines are at the page breaks for the OCRed output. So, each one represents a page break.

    Code:
    ------File: 001x.png---------------------------------------------
    
    FRANKLIN.
    
    A SKETCH
    
    JOHN BIGELOW.
    
    Price 25 Cents]
    ------File: 001.png---------------------------------------------
    
    ------File: 001.png---------------------------------------------
    
    FRANKLIN
    
    A SKETCH.
    
    BY JOHN BIGELOW.
    
    BOSTON: LITTLE, BROWN, & CO.
    
    1879.
    ------File: 001.png---------------------------------------------
    
    Entered according to Act of Congress, in the year 1878, by
    
    LITTLE, BROWN, & COMPANY,
    
    in the office of the Librarian of Congress at Mashington.
    ------File: 001.png---------------------------------------------
    
    FRANKLIN.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Way to specify the actual problem.

    Code:
    import os
    import re
    original = 'mytextfile.txt'
    backup = original+'.bak'
    os.rename(original,backup)           # preserve a copy of the original
    match = re.compile('---+File:').match  # handle your carelessness about hyphen count
    with open(backup,'r') as inf:
        with open(original,'w') as ouf:
            n = 1                 # I assume the first match is numbered 1
            for line in inf:
                fields = line.split()
                if not ((2 == len(fields)) and (match(fields[0]))):
                    ouf.write(line)
                else:
                    of = fields[1].split('.')
                    ouf.write('%s %03d.%s\n'%(fields[0],n,of[1]))
                    n += 1
    ps. overwriting the input file is just about always foolish, stupid, and a pain of the arse. The backup I've made helps little. Suppose you test the program twice in a row forgetting to copy the backup file to the input. You've lost your input and the whole world sux.
    Last edited by b49P23TIvg; September 4th, 2012 at 04:28 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    tom_cos
    Guest
    Devshed Newbie (0 - 499 posts)

    Woo Hoo!


    Yippie! That worked like a charm!

    Excellent!!

    Tom C.

IMN logo majestic logo threadwatch logo seochat tools logo