Add text to beggining of document program - problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Add text to beggining of document program - problem

 
0
  #1
Feb 5th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: micdareall is an unknown quantity at this point 
Solved Threads: 0
micdareall micdareall is offline Offline
Newbie Poster

Re: Add text to beggining of document program - problem

 
0
  #2
Feb 6th, 2008
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
  1. pickle.dump(joinList, file)
with...
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Add text to beggining of document program - problem

 
0
  #3
Feb 6th, 2008
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.
  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()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: micdareall is an unknown quantity at this point 
Solved Threads: 0
micdareall micdareall is offline Offline
Newbie Poster

Re: Add text to beggining of document program - problem

 
0
  #4
Feb 6th, 2008
With the re module the whole project would be as simple as this...


  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1403 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC