943,808 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1972
  • Python RSS
Feb 5th, 2008
0

Add text to beggining of document program - problem

Expand Post »
I've made a program that can scan through a plain text document for the string "SA001:" and if it's not there, it ads it in at the top. But the problem comes with the saving to the document: It adds some things to the document that I don't know how to get rid of (to see what i mean, point the program to any txt file, tell it to modify it, and then look at the output. ) Also, I'd like it to print a carriage return to the txt file, which I don't know how to do...

Heres the code:
Python Syntax (Toggle Plain Text)
  1. import pickle
  2. import random
  3.  
  4. saFlag = False #<--- flag
  5.  
  6. saLessFile = raw_input("File to scan? ")
  7. #opens the selected file
  8. textf = open(saLessFile, 'r')
  9. str1 = textf.read()
  10. textf.close()
  11.  
  12. #makes every word in file a list
  13. wordlist = str1.split(None)
  14.  
  15. for i in range(0, len(wordlist)):
  16. print wordlist[i],
  17.  
  18. print "\n\nScan loop follows:"
  19.  
  20. for i in range(0, len(wordlist)):
  21. if (wordlist[i].lower() == "sa001:"):
  22. print "SA001 found:"
  23. saFlag = True
  24. for j in range(i, (i+7)):
  25. print wordlist[j],
  26. print "..."
  27.  
  28. if saFlag:
  29. print "No altering needed\n"
  30.  
  31. else:
  32. print "SA not found turn document into the following?"
  33. wordlist.insert(0, "SA001: This is a test, blah blah blah blah blah!\n")
  34.  
  35. #turns the list into a string with a space inbetween each word
  36. joinList = " ".join(wordlist)
  37. print joinList
  38.  
  39. #asks user if they are sure they wish to modify
  40. sure = raw_input("Modify?(y or n): ")
  41.  
  42. if sure == "y" or sure == "Y":
  43. #saves the new string over the old file's words, essentially modifying the contents
  44. file = open(saLessFile, "w")
  45. pickle.dump(joinList, file)
  46. file.close()
  47. print "modified..."
  48. elif sure == "n" or sure == "N":
  49. quit
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008
Feb 6th, 2008
0

Re: Add text to beggining of document program - problem

Your problem is the use of the pickle module. Your using it to dump out the plain text data to a file and that is not the purpose of pickling.. Read up on what pickling actually is.
http://docs.python.org/lib/module-pickle.html

If you replace
Python Syntax (Toggle Plain Text)
  1. pickle.dump(joinList, file)
with...
Python Syntax (Toggle Plain Text)
  1. file.write(joinList)
It will work to an extent except the fact that you lose all newline characters from the original text.


I would also make the recommendation that you use the re module to do a search through the file for the text. Or atleast keep a copy of the original text before you split it and then just concatenate the 'sa001:' with the original file content before dumping it all out to the file.


edit: '\n' is the newline character.
Last edited by micdareall; Feb 6th, 2008 at 2:27 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
micdareall is offline Offline
3 posts
since Dec 2006
Feb 6th, 2008
0

Re: Add text to beggining of document program - problem

You can also read all of the file into memory, process it once to search for the string, and then process it a second time to write it to a file if necessary.
Python Syntax (Toggle Plain Text)
  1. data = open(filename, "r").readlines()
  2. found = 0
  3. for rec in data:
  4. if ("sa001:" in rec) or ("SA001:") in rec:
  5. print "SA001 found"
  6. found =1
  7.  
  8. ## file only changes if string is not found
  9. if not found:
  10. fp = open(filename+".2", "w")
  11. fp.write( "SA001:\n") ## add this line
  12. for rec in data: ## original data
  13. fp.write(rec)
  14. fp.close()
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Feb 6th, 2008
0

Re: Add text to beggining of document program - problem

With the re module the whole project would be as simple as this...


Python Syntax (Toggle Plain Text)
  1. import re
  2. mFilename = raw_input("File to scan? ")
  3. mFile= open(mFilename).read()
  4.  
  5.  
  6. print "Searching File..."
  7. #the re.findall function
  8. #first parameter : string/regular expression/pattern to search for
  9. #second parameter: string to search in
  10. #third is optional: I passed in re.ignorecase to tell the module to ignore the case
  11. if not re.findall('SA001:',mFile,re.IGNORECASE):
  12. #'SAOO1: is not in the file
  13. print "Text not found..."
  14. sure = raw_input("Modify?(y or n): ")
  15. if sure == "y" or sure == "Y":
  16. open(mFilename,'w').write("SA001:\n"+mFile)
  17. print "File Modified..."
  18. else:
  19. #'SAOO1: is already in the file
  20. print "No Altering needed."
Last edited by micdareall; Feb 6th, 2008 at 5:52 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
micdareall is offline Offline
3 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Starting a program at the computer's boot-up
Next Thread in Python Forum Timeline: Making a randomly generated PIL wallpaper





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC