1

I have a plain text HTML file and am trying to create a Python script that amends this file.

One of the lines reads:

var myLatlng = new google.maps.LatLng(LAT,LONG);

I have a little Python script that goes off and grabs the co-ordinates of the International Space Station. I then want it to amend a file to add the Latitude and Longitude.

Is it possible to use RegEx and parse just that one line? I don't fancy parsing the entire file. If it is possible, which module would be best to use? and how would I point it at that line?

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
  • 1
    Does your program generate the HTML file? If not, may have better luck with an [HTML parser](https://docs.python.org/3/library/html.parser.html) or [XML parser](https://docs.python.org/2/library/xml.dom.minidom.html). – Seth Mar 31 '14 at 18:04
  • possible duplicate of [Search and replace a line in a file in Python](http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python) – kojiro Mar 31 '14 at 18:06
  • I know it may not seem like they're quite the same question, but I think ultimately they are, and the *most upvoted* (not the accepted) answer there is spot-on for this problem. – kojiro Mar 31 '14 at 18:07

2 Answers2

0

If I understand you correctly, you have an HTML file with the line that you wrote out above. You want to replace the (LAT,LONG) part with the actual lat and long values that your python script will find.

If that's correct, then I would recommend going ahead and writing the HTML file to a .txt file:

import urllib
import time 

while True: 

    open = urllib.urlopen(the_url_where_the_html_comes_from)
    html = open.read()

    my_file = open("file.txt","w")
    my_file.write(html)
    my_file.close()

    #you don't need any fancy modules or RegEx to edit one unique line. 
    my_file = open("file.txt","r+")
    text = my_file.read()
    text.replace("LatLng(LAT,LONG)","LatLng("+lat_variable+","+long_variable+")")
    real_text = text
    my_file.close()

    #now you want the change that you made to remain in that file
    my_file = open("file.txt","w")
    my_file.write(real_text)
    my_file.close()

    #if you check "file.txt", it should have those values replaced. 

    time.sleep(However long until the html updates)

I haven't tested this code, so let me know if it works or not!

EDIT: If the HTML file is constantly changing, then you could use the urllib module to update it. See above code.

kttr
  • 414
  • 1
  • 3
  • 11
  • 2
    Thanks for the speedy reply. That looks excellent. The only problem is the html file will be constantly changing. That code would work once, but then how would I replace the co-ordinates a second time? – user3482179 Mar 31 '14 at 18:41
  • Try having a "template" html file which always has "LatLng(LAT, LONG) in it. You can then use that template file in the code each time and overwrite the file you are actually going to use on the website. – austin-schick Mar 31 '14 at 19:01
0

Thanks so much for the help. This website is awesome.

I used the info provided here and did a little bit of reading. I solved the problem by using the following:

#Open the html file for writing
o = open("index.html","w")
#Open the html template file, replace the variables in the code.  
line in open("template"):
line = line.replace("$LAT",lat)
line = line.replace("$LON",lon)
#Write the variables to the index.html file
o.write(line + "\n")
#Close the file
o.close()

Thanks again