Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 1st, 2012, 02:05 PM
tom_cos's Avatar
tom_cos tom_cos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: NJ, USA
Posts: 72 tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 20 h 9 m 51 sec
Reputation Power: 47
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.

Reply With Quote
  #2  
Old September 1st, 2012, 05:04 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old September 4th, 2012, 04:14 PM
tom_cos's Avatar
tom_cos tom_cos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: NJ, USA
Posts: 72 tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 20 h 9 m 51 sec
Reputation Power: 47
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.

Reply With Quote
  #4  
Old September 4th, 2012, 04:25 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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.

Reply With Quote
  #5  
Old September 5th, 2012, 08:42 AM
tom_cos's Avatar
tom_cos tom_cos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Location: NJ, USA
Posts: 72 tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level)tom_cos User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 20 h 9 m 51 sec
Reputation Power: 47
Woo Hoo!

Yippie! That worked like a charm!

Excellent!!

Tom C.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Search Replace Image Numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap