[SOLVED] help with escaping characters
#1
I'm trying to change some text in a file using the re.sub command.
i'm trying to change this "event": { (quotes and curly bracket are included)
into this "event": [{ i am just trying to add the bracket basically.


PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)
        
test re.sub""event": {"""event": [{",response )
        
results json.loads(test)
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

I have tried using the \ character as an escape for the quotes and the curly bracket but it doesn't seem to escape them. What am I doing wrong?

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)
        
test re.sub"\"event\": \{"""event": [{",response )
        
results json.loads(test)
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

I also want to change }}} into this }]}} adding a bracket after the first curly bracket but I am having a heck of a time trying to figure out how to match these characters. Is there a command to tell it to just match exactly what I have and to just ignore the meaning of the brackets, curly brackets and quotes? some kind of command to match what I want exactly how I have written it?... I am going nuts trying to do something that seems simple but I can't seem to wrap my head around this regex stuff. Thanks for any help anyone could give me.
Reply
#2
Wouldn't it be easier to do the transformation after you've loaded and parsed the JSON?
Reply
#3
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.
Reply
#4
(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_apikeyurl)
        
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.
Reply
#5
I take it from that your script isn't actually processing the response, just passing it off to another addon? In that case, something like the following should work

Code:
response.replace( '"event": {', '"event": [{' ).replace( '}}}', '}]}}' )

Though the ideal solution is always to actually process these things yourself, checking whether event contains a list or not (though probably not something you want to start doing if you're just handing the data off)
Reply
#6
(2014-06-20, 11:40)Unfledged Wrote: I take it from that your script isn't actually processing the response, just passing it off to another addon? In that case, something like the following should work

Code:
response.replace( '"event": {', '"event": [{' ).replace( '}}}', '}]}}' )

Though the ideal solution is always to actually process these things yourself, checking whether event contains a list or not (though probably not something you want to start doing if you're just handing the data off)

I'm lost as to how I would make that work... would it be like this
PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)
        
response.replace'"event": {''"event": [{' ).replace'}}}''}]}}' )
        
results json.loads(response)
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

I tried it and it didn't give me no error or nothing but it didn't change the file at all. It leaves it as is like below...
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 tried to just replace the 3 curlys }}} to }]}} like this...
PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)#.replace( '}}}', '}c}}')
        
response.replace'}}}''}]}}' )
        
results json.loads(response)
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

it ignores or can't even find the three curly brackets and still doesn't change the file at all... it remains the same.. i'm stumped
Reply
#7
I don't know... this sucks... the only way I can get it to change anything in the file is to take out the needed quotes around event in the search pattern...

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url).replace'event''event: [{' )
        
results json.loads(response)
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

but then I end up with a result like this.... "event [{": when I actually need a result like this...."event": [{ I need those quotes around event to be in the search and replace patterns.

It seems to not want any quotes or curly brackets or anything other than straight letters of the alphabet without any other non-alphabet or meta characters at all. Even if I try and use a backslash to escape them, It doesn't change anything in the file no matter what I do unless I use only letters of the alphabet. i.e... event and not "event"... If I try a search pattern with the 3 curly brackets or even just put 1 curly bracket in the search pattern and try to change anything with even just one it doesn't do anything at all... I'm at a loss.
Reply
#8
It works fine here based on the sample you provided, but that sample can't be loaded by simplejson. Either way, if that's not working for you then try doing it with the json itself. Something like the following (assuming you're using simplejson - and obviously untested on your data as I can't get that to load in simplejson)

PHP Code:
results json.loads(response)
# Check if the event data acts like a list
if typeresults['event'] ) is not list:
    
# It doesn't, so put it into a list
    
results['event'] = [ results['event'] ] 

Should add the square braces in the right place, and be a lot cleaner than doing a search and replace. (Though there's every chance that results['event'] isn't the json element you'll want to modify. You may need to play around to make sure you're modifying the right one.)
Reply
#9
(2014-06-20, 16:31)Unfledged Wrote: It works fine here based on the sample you provided, but that sample can't be loaded by simplejson. Either way, if that's not working for you then try doing it with the json itself. Something like the following (assuming you're using simplejson - and obviously untested on your data as I can't get that to load in simplejson)

PHP Code:
results json.loads(response)
# Check if the event data acts like a list
if typeresults['event'] ) is not list:
    
# It doesn't, so put it into a list
    
results['event'] = [ results['event'] ] 

Should add the square braces in the right place, and be a lot cleaner than doing a search and replace. (Though there's every chance that results['event'] isn't the json element you'll want to modify. You may need to play around to make sure you're modifying the right one.)
I tried this...

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)
        
results json.loads(response)
        
# Check if the event data acts like a list
        
if typeresults['event'] ) is not list:
            
# It doesn't, so put it into a list
            
results['event'] = [ results['event'] ]
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

But I'm getting this error in the log...

Error Type: <type 'exceptions.KeyError'>
Error Contents: ('event',)
... ... Traceback stuff... ...

if type( results['event'] ) is not list:
KeyError: ('event',)
-->End of Python script error report<--
Reply
#10
(2014-06-20, 17:18)woodside Wrote:
(2014-06-20, 16:31)Unfledged Wrote: It works fine here based on the sample you provided, but that sample can't be loaded by simplejson. Either way, if that's not working for you then try doing it with the json itself. Something like the following (assuming you're using simplejson - and obviously untested on your data as I can't get that to load in simplejson)

PHP Code:
results json.loads(response)
# Check if the event data acts like a list
if typeresults['event'] ) is not list:
    
# It doesn't, so put it into a list
    
results['event'] = [ results['event'] ] 

Should add the square braces in the right place, and be a lot cleaner than doing a search and replace. (Though there's every chance that results['event'] isn't the json element you'll want to modify. You may need to play around to make sure you're modifying the right one.)
I tried this...

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)#.replace( 'event', 'event [{' )
        
results json.loads(response)
        
# Check if the event data acts like a list
        
if typeresults['event'] ) is not list:
            
# It doesn't, so put it into a list
            
results['event'] = [ results['event'] ]
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

But I'm getting this error in the log...

Error Type: <type 'exceptions.KeyError'>
Error Contents: ('event',)
... ... Traceback stuff... ...

if type( results['event'] ) is not list:
KeyError: ('event',)
-->End of Python script error report<--

As I said, there's every chance that results['event'] isn't the json element you'll want to modify. Now we know for a fact that it isn't. Maybe look into iterating over python dictionaries to find the right element - and there's also a good chance it'll be an element within an element - results['key 1']['event'].
Reply
#11
(2014-06-20, 17:22)Unfledged Wrote:
(2014-06-20, 17:18)woodside Wrote:
(2014-06-20, 16:31)Unfledged Wrote: It works fine here based on the sample you provided, but that sample can't be loaded by simplejson. Either way, if that's not working for you then try doing it with the json itself. Something like the following (assuming you're using simplejson - and obviously untested on your data as I can't get that to load in simplejson)

PHP Code:
results json.loads(response)
# Check if the event data acts like a list
if typeresults['event'] ) is not list:
    
# It doesn't, so put it into a list
    
results['event'] = [ results['event'] ] 

Should add the square braces in the right place, and be a lot cleaner than doing a search and replace. (Though there's every chance that results['event'] isn't the json element you'll want to modify. You may need to play around to make sure you're modifying the right one.)
I tried this...

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)#.replace( 'event', 'event [{' )
        
results json.loads(response)
        
# Check if the event data acts like a list
        
if typeresults['event'] ) is not list:
            
# It doesn't, so put it into a list
            
results['event'] = [ results['event'] ]
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 

But I'm getting this error in the log...

Error Type: <type 'exceptions.KeyError'>
Error Contents: ('event',)
... ... Traceback stuff... ...

if type( results['event'] ) is not list:
KeyError: ('event',)
-->End of Python script error report<--

As I said, there's every chance that results['event'] isn't the json element you'll want to modify. Now we know for a fact that it isn't. Maybe look into iterating over python dictionaries to find the right element - and there's also a good chance it'll be an element within an element - results['key 1']['event'].

lol... now I'm completely confused. I have no clue really of what i'm doing as I have only started digging into python a couple of weeks or so ago. So I haven't a clue on how to do any of that stuff... Wow... this seems so simple of a thing to do but it is a nightmare to me... lol
Reply
#12
Tell you what, put the following in after your json.loads line - it'll print the json to the debug log. Copy and paste it here, and from that we should be able to see which json element you're going to want to modify Smile

Code:
print( repr( results ) )
Reply
#13
oh my gosh... Sucess!!!
I tried it like this replacing "Key 1" with "events" and it works...lol
It loads up with one event or many events... I'm flabbergasted!!!
Bro... I really really really appreciate your help. I can't thank you enough for your time and effort you put into helping me with my problem. Thanks a whole lot.

PHP Code:
else:
        
url 'http://ws.audioscrobbler.com/2.0/?api_key=%s&format=json&%s' % (lastfm_apikeyurl)
        
response GetStringFromUrl(url)
        
results json.loads(response)
        if 
typeresults['events']['event'] ) is not list:
            
results['events']['event'] = [ results['events']['event'] ]
        
save_to_file(results,filename,Addon_Data_Path)
        return 
results 
Reply
#14
Glad you got it sorted Smile
Reply

Logout Mark Read Team Forum Stats Members Help
[SOLVED] help with escaping characters0