nits nits - 2 months ago 10
Python Question

SQL Query output formatting URL in python

I am new to python. I am writing a script which queries the database for a URL string. Below is my snippet.

db.execute('select sitevideobaseurl,videositestring '
'from site, video '
'where siteID =1 and site.SiteID=video.VideoSiteID limit 1')
result = db.fetchall()

for row in result:
videosite= row[0:2]
print videosite


It gives me baseURL from a table and the video site string from another table.
output:
('http://www.youtube.com/watch?v={0}', 'uqcSJR_7fOc')


I wish to format the output by removing the braces, quotes and commas and replace the {0} from baseURL with sitestring: uqcSJR_7fOc.
something like: https://www.youtube.com/watch?v=uqcSJR_7fOc in the final output and wish to write this to a file.

Thanks for your time and help in advance.

Answer

Use str.format.

db.execute('select sitevideobaseurl,videositestring '
          'from site, video '
          'where siteID =1 and site.SiteID=video.VideoSiteID limit 1')
result = db.fetchall()

for row in result:
  videosite= row[0:2]
  print videosite[0].format(videosite[1])