The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Search Replace Image Numbers
Discuss Search Replace Image Numbers in the Python Programming forum on Dev Shed. Search Replace Image Numbers Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 1st, 2012, 02:05 PM
|
 |
Contributing User
|
|
Join Date: Jun 2007
Location: NJ, USA
|
|
|
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.
|

September 1st, 2012, 05:04 PM
|
 |
Contributing User
|
|
|
|
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!
|

September 4th, 2012, 04:14 PM
|
 |
Contributing User
|
|
Join Date: Jun 2007
Location: NJ, USA
|
|
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.
|

September 4th, 2012, 04:25 PM
|
 |
Contributing User
|
|
|
|
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.
|

September 5th, 2012, 08:42 AM
|
 |
Contributing User
|
|
Join Date: Jun 2007
Location: NJ, USA
|
|
|
Woo Hoo!
Yippie! That worked like a charm!
Excellent!!
Tom C.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|