|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hi there, im trying to create a python program that can read a text file line by line and search for specified words/text/strings and remove them from the text file. Then finally save the modified text file to an output file. The only problem is, the text file contains code for a BACKSPACE typed in the text. e.g. "<BACKSPACE>" this needs to be removed which sounds quite simple, but often there are numbers involved. e.g. "<BACKSPACE: 4>" which would represent 4 backspaces, and so the string needs to be removed and 4 backspaces take place on the text before the code. e.g. "repatre<BACKSPACE: 4>resent" would become "represent" I know a search and delete script that can search for a word at the beggining of the line and then delete the whole line, but im not sure about this. Any ideas would be great.
Cheers, Joe |
| Sponsored Links |
|
|
|
|||
|
Quote:
I have been having a look at the re library and have (with a little help off a guy on another forum) come up with a program, that seems logical but im having problems writing the results to a txt file, or even to the screen. I know the basics off creating variables for the input/output txt files but still no luck. |
|
|||
|
Quote:
Joe |
| Sponsored Links |
|
|
|
|||
|
Quote:
Here is a solution (so far seems to work.) I can write results to a txt file which is great. All I need to do now is read from a text which im guessing will just go in place of the 'test' section in the code and to be able to iterate this code so that I can run through a whole paragraph containing multiple <BACKSPACE> sections removing all (rather than getting to the first one and terminating.) Would you have any ideas looking at this to do such a thing? Any help would be great. Cheers. Joe Code:
#! /usr/bin/python
import re
# Global variable
bs = re.compile('<BACKSPACE(:[ ]*[0-9]+)?>')
#theInFile = open("test2.txt", "r")
theOutFile = open("backspace_out.txt", "w")
tests = ['re <BACKSPACE>present', 'This is bound to repatreaaaabbbbcccc <BACKSPACE: 17>resent']
def bs_remove(s):
global bs
for m in bs.finditer(s):
if m.groups()[0] is None:
return s[:m.start() - 1] + s[m.end():]
else:
return s[:m.start() - int(m.groups()[0][1:])] + s[m.end():]
for s in tests:
theOutFile.write (bs_remove(s)+ " ")
|
|
|||||
|
Code:
import re
def remove_backspace(in_filename, out_filename):
in_file = open(in_filename,'r') # Open the input file
out_file = open(out_filename,'w') # Open the output File
r = re.compile('<BACKSPACE(:[ ]*[0-9]+)?>') # The regex
for line in in_file: # Iterates over the lines of the input file
line_without_backspace = r.sub('', line) # Replace every match of the regex with an empty string
out_file.write( line_without_backspace ) # Write the resulting line to the file
in_file.close() # Close input file
out_file.close() # Close output file
Last edited by monkey_instinct; 01-26-2008 at 03:07 PM. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to create a text file on my host and save the input of my form there ? | kresh7 | Visual Basic Programming | 2 | 11-27-2007 04:00 PM |
| How to save the text in a text file ??? | kresh7 | Visual Basic Programming | 0 | 11-25-2007 11:30 AM |
| How to style fonts of a text in a simple page? | c0de | Tutorials | 3 | 09-15-2007 11:08 PM |
| text file manipulations in vb6.0 | Ronin_paes | Visual Basic Programming | 3 | 06-11-2007 05:54 AM |
| Program to pass data from text file to table. | sania21 | Java Help | 3 | 05-28-2007 09:32 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |