"simple" file parser i am making

Reply

Join Date: Jul 2008
Posts: 3
Reputation: dz0004455 is an unknown quantity at this point 
Solved Threads: 0
dz0004455 dz0004455 is offline Offline
Newbie Poster

"simple" file parser i am making

 
0
  #1
Jul 14th, 2008
I have an html file and I am parsing it with regular expressions. I don't want to use any python html libraries, because I am not using it as a website, but as a configuration type file. I am making a basic bookmarks system, the parser works fine, so I guess my question is about the second part, writing to the files. The file looks like this
  1. <html>
  2. <head>
  3. <title>my bookmarks</title>
  4. </head>
  5. <body>
  6. <div id="smalltalk">
  7. <a href="http://www.smalltalk.org">Smalltalk.org</a>
  8. </div>
  9. <div id="python">
  10. <a href="http://www.python.org">Python.org</a>
  11. </div>
  12. </body>
  13. </html>
My parser call the div's sections, and I want to write a method to insert more sections before the /body and /html tags.

The question, without all of my other information is, how do I INSERT text into a file with python, without deleting the text after it.
Last edited by Tekmaven; Jul 14th, 2008 at 11:00 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: Capt.Micro is an unknown quantity at this point 
Solved Threads: 1
Capt.Micro Capt.Micro is offline Offline
Newbie Poster

Re: "simple" file parser i am making

 
0
  #2
Jul 14th, 2008
Well, I dont know where you want to insert the text, aka: between the <body> and </body> or between the </body> and </html> but, if you need to write at a specific line you could always copy the text before you write to it, then append the file with open(file, 'a') and then add the text you have copied before, maybe like so(write after the </body> tag):
  1. FILE = open('file.txt', 'a')
  2. lines = FILE.readlines()
  3. lineNum=0
  4. currLine=0
  5. #Find the </body> line
  6. for line in lines:
  7. if line == "</body>":
  8. lineNum=currLine
  9. break
  10. currLine+=1
  11. #Write to the list
  12. lines.insert(lineNum+1, "Data, text, or anything you need to insert!")
  13. FILE.close()
  14. #Open file and clear it
  15. FILE = open('file.txt', 'w')
  16. #Write old data and new data (in list: lines)
  17. for line in lines:
  18. FILE.write(line)
  19. FILE.close()
Last edited by Capt.Micro; Jul 14th, 2008 at 1:28 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: dz0004455 is an unknown quantity at this point 
Solved Threads: 0
dz0004455 dz0004455 is offline Offline
Newbie Poster

Re: "simple" file parser i am making

 
0
  #3
Jul 14th, 2008
Thank you, I didn't see an insert method anywhere on the python reference! I will see if the code works, then get back to you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: dz0004455 is an unknown quantity at this point 
Solved Threads: 0
dz0004455 dz0004455 is offline Offline
Newbie Poster

Re: "simple" file parser i am making

 
0
  #4
Jul 14th, 2008
o, i see what you are doing, i cant quite get it to work, but, thank you for the help!
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC