| | |
"simple" file parser i am making
![]() |
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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.
html Syntax (Toggle Plain Text)
<html> <head> <title>my bookmarks</title> </head> <body> <div id="smalltalk"> <a href="http://www.smalltalk.org">Smalltalk.org</a> </div> <div id="python"> <a href="http://www.python.org">Python.org</a> </div> </body> </html>
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
•
•
Join Date: May 2008
Posts: 10
Reputation:
Solved Threads: 1
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):
python Syntax (Toggle Plain Text)
FILE = open('file.txt', 'a') lines = FILE.readlines() lineNum=0 currLine=0 #Find the </body> line for line in lines: if line == "</body>": lineNum=currLine break currLine+=1 #Write to the list lines.insert(lineNum+1, "Data, text, or anything you need to insert!") FILE.close() #Open file and clear it FILE = open('file.txt', 'w') #Write old data and new data (in list: lines) for line in lines: FILE.write(line) FILE.close()
Last edited by Capt.Micro; Jul 14th, 2008 at 1:28 am.
![]() |
Other Threads in the Python Forum
- Previous Thread: float to int
- Next Thread: Get Current Active Window Title/Name
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv bash beginner bits calling casino changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically enter event events examples external file float format frange function google gui hints homework i/o iframe import input jaunty java lapse line linux list lists loop matching mouse multiple newb number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning string strings tails terminal text threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython





