(2014-06-20, 09:42)Unfledged Wrote: The curly braces shouldn't need escaping in this situation, so it's all about how you use your quotation marks. As you're wanting to match double quotes, use single quotes to contain the different strings you're passing to re.sub and use the double quotes exactly as they appear in the string you're matching. Something like the following should work:
Code:
re.sub( '"event": {', '"event": [{', response)
However, it'll be a lot easier to parse the JSON first and then load the data into a list. Also, you're using pythons regex function, but matching something that could just as easily be done with the replace function.
I tried doing it after jsons loads before json loads, using "replace" using "re.sub" and several other different ways but with no luck either way.
Still can't seem to make it work. I tried this with your suggestion...
PHP Code:
else:
url = 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikey, url)
response = GetStringFromUrl(url)
test = re.sub( '"event": {', '"event": [{', response)
results = json.loads(test)
save_to_file(results,filename,Addon_Data_Path)
return results
It doesn't give no error with your suggestion, but it doesn't change the file at all, everything is the same.
A little background of the problem i have. When I run the script and there is more than one concert event it loads to the skin with no problems, however when there is only one event the url returned is missing a couple of brackets a [ at the point in the file just before the curly bracket to the left of "{"website as seen here... "event": {"website": if I manually add the bracket like this..."event": [{"website": and also at the very end of the file there are 3 curly brackets }}} it is missing a bracket right after the first curly bracket it should be like this }]}}. If I manually add those brackets it loads fine with just the one event. So what I am trying to do is use the re.sub or replace commands to add those 2 brackets to where they are needed. Here is the file that is return when the artist has only one event. It is missing the [ bracket in this part... "event": {"website": and also missing the closing bracket at the very end of the file where the 3 curly brackets are }}}... it should have a ] right after the first of the three brackets as in }]}}
The bad file...without the 2 needed brackets...
Quote:{"events": {"@attr": {"perPage": "50", "artist": "Black Oak Arkansas", "festivalsonly": "0", "totalPages": "1", "total": "1", "page": "1"}, "event": {"website": "https://st94.com/store/view-item-detail.aspx?ItemID=BOA.7.3", "startDate": "Thu, 03 Jul 2014 20:00:00", "attendance": "0", "description": "<div class=\"bbcode\">The world's primo southern hillbilly psycho boogie band with over 40 years of history returns with a new release from Atlantic/Atco Records \u2018Back Thar N\u2019 Over Yonder\u2019 and features reunited originals Jim \u201cDandy\u201d Mangrum, Rickie Lee Reynolds and Pat \u201cDirty\u201d Daugherty. Boasting the first ever three guitar lineup, Black Oak Arkansas earned three gold albums selling close to five million records and headlined arenas in the 1970s. Elvis Presley encouraged them to perform the LaVerne Baker classic \u201cJim Dandy to the Rescue\u201d which became their signature song.</div>", "title": "Black Oak Arkansas", "url": "http://www.last.fm/event/3836743+Black+Oak+Arkansas+at+Sellersville+Theater+on+3+July+2014", "tickets": "\n ", "image": [{"#text": "http://userserve-ak.last.fm/serve/34/51567.jpg", "size": "small"}, {"#text": "http://userserve-ak.last.fm/serve/64/51567.jpg", "size": "medium"}, {"#text": "http://userserve-ak.last.fm/serve/126/51567.jpg", "size": "large"}, {"#text": "http://userserve-ak.last.fm/serve/252/51567.jpg", "size": "extralarge"}], "tags": {"tag": ["blues rock", "southern rock", "classic rock", "rock"]}, "venue": {"website": "http://ST94.com", "name": "Sellersville Theater", "url": "http://www.last.fm/venue/9133038+Sellersville+Theater", "image": [{"#text": "http://userserve-ak.last.fm/serve/34/95990155.jpg", "size": "small"}, {"#text": "http://userserve-ak.last.fm/serve/64/95990155.jpg", "size": "medium"}, {"#text": "http://userserve-ak.last.fm/serve/126/95990155.jpg", "size": "large"}, {"#text": "http://userserve-ak.last.fm/serve/252/95990155.jpg", "size": "extralarge"}, {"#text": "http://userserve-ak.last.fm/serve/500/95990155/Sellersville+Theater+ST2013.jpg", "size": "mega"}], "phonenumber": "215-257-5808", "location": {"postalcode": "18960", "city": "Sellersville, PA", "geo:point": {"geo:long": "-75.311631", "geo:lat": "40.359344"}, "street": "24 W. Temple Ave.", "country": "United States"}, "id": "9133038"}, "reviews": "0", "tag": "lastfm:event=3836743", "artists": {"headliner": "Black Oak Arkansas", "artist": "Black Oak Arkansas"}, "cancelled": "0", "id": "3836743"}}}
If I add those two brackets and make the file like this it loads up with no problem with just the one concert event.
the good file with the 2 needed brackets.
Quote:{"events": {"@attr": {"perPage": "50", "artist": "Black Oak Arkansas", "festivalsonly": "0", "totalPages": "1", "total": "1", "page": "1"}, "event": [{"website": "https://st94.com/store/view-item-detail.aspx?ItemID=BOA.7.3", "startDate": "Thu, 03 Jul 2014 20:00:00", "attendance": "0", "description": "<div class=\"bbcode\">The world's primo southern hillbilly psycho boogie band with over 40 years of history returns with a new release from Atlantic/Atco Records \u2018Back Thar N\u2019 Over Yonder\u2019 and features reunited originals Jim \u201cDandy\u201d Mangrum, Rickie Lee Reynolds and Pat \u201cDirty\u201d Daugherty. Boasting the first ever three guitar lineup, Black Oak Arkansas earned three gold albums selling close to five million records and headlined arenas in the 1970s. Elvis Presley encouraged them to perform the LaVerne Baker classic \u201cJim Dandy to the Rescue\u201d which became their signature song.</div>", "title": "Black Oak Arkansas", "url": "http://www.last.fm/event/3836743+Black+Oak+Arkansas+at+Sellersville+Theater+on+3+July+2014", "tickets": "\n ", "image": [{"#text": "http://userserve-ak.last.fm/serve/34/51567.jpg", "size": "small"}, {"#text": "http://userserve-ak.last.fm/serve/64/51567.jpg", "size": "medium"}, {"#text": "http://userserve-ak.last.fm/serve/126/51567.jpg", "size": "large"}, {"#text": "http://userserve-ak.last.fm/serve/252/51567.jpg", "size": "extralarge"}], "tags": {"tag": ["blues rock", "southern rock", "classic rock", "rock"]}, "venue": {"website": "http://ST94.com", "name": "Sellersville Theater", "url": "http://www.last.fm/venue/9133038+Sellersville+Theater", "image": [{"#text": "http://userserve-ak.last.fm/serve/34/95990155.jpg", "size": "small"}, {"#text": "http://userserve-ak.last.fm/serve/64/95990155.jpg", "size": "medium"}, {"#text": "http://userserve-ak.last.fm/serve/126/95990155.jpg", "size": "large"}, {"#text": "http://userserve-ak.last.fm/serve/252/95990155.jpg", "size": "extralarge"}, {"#text": "http://userserve-ak.last.fm/serve/500/95990155/Sellersville+Theater+ST2013.jpg", "size": "mega"}], "phonenumber": "215-257-5808", "location": {"postalcode": "18960", "city": "Sellersville, PA", "geo:point": {"geo:long": "-75.311631", "geo:lat": "40.359344"}, "street": "24 W. Temple Ave.", "country": "United States"}, "id": "9133038"}, "reviews": "0", "tag": "lastfm:event=3836743", "artists": {"headliner": "Black Oak Arkansas", "artist": "Black Oak Arkansas"}, "cancelled": "0", "id": "3836743"}]}}
I have read and read and read but I can't seem to just add a couple of brackets to a file using python before it loads into the skin, it seems simple enough but
I just happen to have to add characters that are also used in python. Thanks ahead for any more help you could give me.