Search code examples
pythonxbmc

How do I return string argument without brackets and quotes in Python


I'm very new to Python and so am struggling with what is probably very simply to a seasoned Python programmer.

The problem below is that when the groupid argument is supplied with a value, it is evaluated to ['groupid'].

Can someone please explain how I concatenate the string so that it simply has the groupid value without the square brackets and singles quotes?

Many thanks,

import sys
import xbmcgui
import xbmcplugin
import urllib
import urlparse

import xbmc, xbmcaddon, xbmcvfs, simplejson, urllib2

base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
args = urlparse.parse_qs(sys.argv[2][1:])

xbmcplugin.setContent(addon_handle, 'movies')

def build_url(query):
    return base_url + '?' + urllib.urlencode(query)

mode = args.get('mode', None)
groupid = args.get('groupid', None)

if (groupid is None): 
    testurl = 'hxxp://webserver.com/json.php'
else:
    testurl = 'hxxp://webserver.com/json.php?group=' + str(groupid)

req = urllib2.urlopen(testurl)
response = req.read()
req.close()

Solution

  • urlparse.parse_qs always returns a dictionary of lists, since a parameter name could be repeated in the query string and returning a list will allow you to see multiple values of the parameter. Since you presumably expect only one value for your query parameter of "groupid", you just want the first element of the list, hence:

    groupid[0]
    

    instead of

    str(groupid)
    

    str(groupid) was printing a string represention of the list.