Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Python

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-2008, 10:14 AM
joe1986 joe1986 is offline
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0
joe1986 is on a distinguished road
Default Text file manipulation.

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-22-2008, 10:42 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

You need to look into regular expressions to search in the way as you want to. Python has a standard library for this, called re. It's pretty simple to use, so it doesn't take long to learn the basics of the functions. There's a lot of tutorials on the internet which can get you started.

Are you having problems with reading/writing files as well?
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-23-2008, 06:25 AM
joe1986 joe1986 is offline
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0
joe1986 is on a distinguished road
Default

Quote:
Originally Posted by v0id View Post
You need to look into regular expressions to search in the way as you want to. Python has a standard library for this, called re. It's pretty simple to use, so it doesn't take long to learn the basics of the functions. There's a lot of tutorials on the internet which can get you started.

Are you having problems with reading/writing files as well?
Cheers for the reply.
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-23-2008, 07:02 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Code:
# Write to screen
print YourVariable
Code:
# Write to file
Filename = "yourfilename"
FileHandle = open(Filename, 'w') # Open the file for writing
FileHandle.write(YourVariable) # Write YourVariable to the file
FileHandle.close() # Close the file again
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-23-2008, 08:02 AM
joe1986 joe1986 is offline
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0
joe1986 is on a distinguished road
Default

Quote:
Originally Posted by v0id View Post
Code:
# Write to screen
print YourVariable
Code:
# Write to file
Filename = "yourfilename"
FileHandle = open(Filename, 'w') # Open the file for writing
FileHandle.write(YourVariable) # Write YourVariable to the file
FileHandle.close() # Close the file again
Thanks for that info. Really helped.
Joe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-23-2008, 08:26 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

If you have other problems, then just ask!

Good luck with your project.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-23-2008, 10:01 AM
joe1986 joe1986 is offline
Newbie
 
Join Date: Sep 2007
Posts: 5
Rep Power: 0
joe1986 is on a distinguished road
Default

Quote:
Originally Posted by v0id View Post
If you have other problems, then just ask!

Good luck with your project.
Hi V0id,

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)+ " ")
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-23-2008, 10:25 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Sorry, but I can't help you on this one. I'm not as good in Python as I've been. I took a look at it, and everything seems fine, but the result isn't like you say yourself. But I haven't managed to find out what's wrong.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-26-2008, 03:02 PM
monkey_instinct's Avatar   
monkey_instinct monkey_instinct is offline
Learning Programmer
 
Join Date: Dec 2007
Posts: 88
Rep Power: 4
monkey_instinct will become famous soon enough
Default

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
I think that will do it.

Last edited by monkey_instinct; 01-26-2008 at 03:07 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-27-2008, 03:48 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

@monkey_instinct: He's not trying to delete every match of the regular expression. If you read his previous posts, you can see he wants the <BACKSPACE X> to delete X characters.
Code:
ABCDEF<BACKSPACE 2>
=>
ABCD
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 08:27 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads