0

I have not been able to figure this out this afternoon so I decided to ask. I am trying to generate some html code with a reference to Google Maps. My output file is a .csv and my delimeter is ;

Python keeps breaking up the Google Maps url line and putting it in another column. Here is the url string that needs to keep the raw &

http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Phoenix+AZ&ie=UTF8&z=12&t=m&iwloc=near&output=embed

I need to preserve the amp; in that url line. I've tried several different codes but nothing has worked. Here is where the code is at in my script.

'<iframe width="425" scrolling="no" height="350" frameborder="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=' + line.split(",")[0]+"" + ' ' + line.split(",")[1]+"" + '&amp;ie=UTF8&amp;z=12&amp;t=m&amp;iwloc=near&amp;output=embed" marginwidth="0" marginheight="0"></iframe>'
Matt
  • 163
  • 1
  • 3
  • 15
  • 1
    Are you sure you need to preserve the `amp;`? Your link seems broken to me (try it). I've just removed the `amp;` part, and actually got [your link working](https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Phoenix+AZ&ie=UTF8&z=12&t=m&iwloc=near&output=embed). You can also substitute special characters in URLs with their ascci codes, prefixed by `%` (`&` --> `%26`). The `urllib`/`urllib2` module might help with encoding, too. – dcsordas Feb 12 '14 at 00:48
  • Your awesome! That's what the problem was. The maps are loading just fine now. Thank you very much. – Matt Feb 12 '14 at 00:58

1 Answers1

1

To escape the ampersands, you need to replace the &s with \&s. I see you tried to escape the ampersands with html character escapes, but that doesn't work the same way in Python. Thus, you need to change &amp; into \&.

Adding backslashes without escaping [Python]

Sorry, I had a brain-fart and somehow thought that ampersands need to be escaped in Python, when they don't. :P

Community
  • 1
  • 1
hydronium
  • 198
  • 3
  • 11