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 November 8th, 2012, 04:37 PM
bigted123 bigted123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 bigted123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 30 sec
Reputation Power: 0
Opening text file, converting one four letter word to another

I am trying to open a saved text file that has a short sentence in it and then convert all four letter words in the sentence to "XXXX". Then save it back to the file. So far I am struggling with this. I pasted what I have done so far, but seem to be stuck now. I'm not sure what is wrong with what I have or if I am just missing something. Also I am new to the forum so if someone can tell me how to post my program properly, I will fix it. Any help would be greatly appreciated!



def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r+')
i = 0
alist = []
for element in file:
words = element.split()
for i in range(len(words)):
if len(words[i]) == 4:
element.replace(words, "XXXX")
i = i+1
alist.append(newwords)
else:
alist.append(words)
print (alist)
file.close()
file1 = open(filename, 'w')
for element in alist:
file.write(alist)
file.close()

Reply With Quote
  #2  
Old November 8th, 2012, 10:26 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 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 13 h 44 m 29 sec
Reputation Power: 383
I'd write the program with flex.

I did not test your program. I'd guess that
1) you have trouble identifying 4 letter words. Your program would remove "cat." from "I ate my cat."
2) You have trouble reconstructing the original.

If I had to use python I'd use the regular expression package,
import re
With flex I'd also use regular expressions---it's so much easier.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 10th, 2012, 07:16 AM
binarydogs binarydogs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 binarydogs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 17 m 46 sec
Reputation Power: 0
Hi Ted,

I have done something similar with Python using regex. I have popped my code below.

Code:
import re
 
file = open("C:\\test.txt", 'r+')
x = file.readlines()
x = str(x)

foo = re.compile(r"""aaaaa""", re.IGNORECASE)
bar = foo.sub('bbbbb', x)
print bar


To format your code snippets you just need to highlight the text and click the # button which will wrap you selection with [CODE] tags.

Quick note - building regex patterns can be rather daunting so I find it helpful to use a piece of open source software called Kodos which you can find http://kodos.sourceforge.net/.

Reply With Quote
  #4  
Old November 10th, 2012, 10:58 AM
KING-OLE KING-OLE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Texas
Posts: 24 KING-OLE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 45 sec
Reputation Power: 0
I think this line is the problem:

element.replace(words, "XXXX")

Try changing that to:

element.replace(words[i], "XXXX")

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Opening text file, converting one four letter word to another

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