Better one;

import re

feedstr = '2014-06-17 13:55:14: IncomingData: TMSUpdateCallback: {
SourceID=JP000456712 Ric=0388.HK BuyQty=3800.0 TradeTime="2014-06-17
13:54:19" DestID="" ExchangeTransCode="65725497 89897456 523 1"
Account="PTBHK01" }';

start = feedstr.find('{');

s = feedstr[start+1:]

def spaceinquotes(match):
        return match.replace(' ', '_')

for quotes in re.findall('"[^"]*"', s):
        s = s.replace(quotes, quotes.replace(' ', '_'))

d = []
for pairs in s.split(" "):
        if pairs.find('=') != -1:
                k,v= pairs.split('=')
                v=v.replace('_', ' ')
                v=v.replace('"', '')
                d.append({k:v})

print d

On Wed, Jun 18, 2014 at 11:19 AM, Girish Venkatachalam
<girishvenkatacha...@gmail.com> wrote:
> import re
>
> feedstr = '2014-06-17 13:55:14: IncomingData: TMSUpdateCallback: {
> SourceID=JP000456712 Ric=0388.HK BuyQty=3800.0 TradeTime="2014-06-17
> 13:54:19" DestID="" ExchangeTransCode="65725497 89897456 523 1"
> Account="PTBHK01" }';
>
> start = feedstr.find('{');
>
> s = feedstr[start+1:]
>
> def spaceinquotes(match):
>         return match.replace(' ', '_')
>
> for quotes in re.findall('"[^"]*"', s):
>         s = s.replace(quotes, quotes.replace(' ', '_'))
>
> d = []
> for pairs in s.split(" "):
>         if pairs.find('=') != -1:
>                 k,v= pairs.split('=')
>                 d.append({k:v})
>
> print d
>
>
> On Tue, Jun 17, 2014 at 9:57 PM, Mohan R <mohan...@gmail.com> wrote:
>> Hello Regex Gurus,
>>
>> I need help handling this particular situation
>>
>> In a log file, I have a line like this,
>>
>> 2014-06-17 13:55:14: IncomingData: TMSUpdateCallback:
>> { SourceID=JP000456712 Ric=0388.HK BuyQty=3800.0 TradeTime="2014-06-17
>> 13:54:19" DestID="" ExchangeTransCode="65725497 89897456 523 1"
>> Account="PTBHK01" }
>>
>> I have to make a dictionary out of this line using the key=value pair
>> strings between {} parenthesis. I can take out the inner string using
>> the following code,
>>
>> re.search('^.*{ (.*) }.*$', line).groups()[0]
>>
>> But, after this, I'm looking for a way to split the string using '
>> '(space), but the values also contains ' '(space) in between. I have to
>> replace the space in the values and split the string using ' '(space).
>> Is there any way in regex to capture only the values which are inside ""
>> and contain space?
>>
>> Thanks,
>> Mohan R
>>
>> _______________________________________________
>> ILUGC Mailing List:
>> http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
>> ILUGC Mailing List Guidelines:
>> http://ilugc.in/mailinglist-guidelines
>
>
>
> --
> Gayatri Hitech
> http://gayatri-hitech.com



-- 
Gayatri Hitech
http://gayatri-hitech.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
ILUGC Mailing List Guidelines:
http://ilugc.in/mailinglist-guidelines

Reply via email to