Re: Splitting string into dictionary

2005-07-02 Thread Christopher Subich
Robert Kern wrote: > David Pratt wrote: > >> I have string text with language text records that looks like this: >> >> 'en' | 'the brown cow' | 'fr' | 'la vache brun' > translations = [x.strip(" '") for x in line.split('|')] > d = dict(zip(translations[::2], translations[1::2])) One caevat is th

Re: Splitting string into dictionary

2005-07-01 Thread John Machin
David Pratt wrote: > I have string text with language text records that looks like this: > > 'en' | 'the brown cow' | 'fr' | 'la vache brun' Pardonnez-moi, but I thought "how now brown cow" translated into something like "comme maintenant vache brune" -- something about the adjectives agreeing

Re: Splitting string into dictionary

2005-07-01 Thread Terry Hancock
On Friday 01 July 2005 12:35 am, David Pratt wrote: > Wow Robert that is incredible python magic! I am trying to figure out > what this is doing since my attempts were regex and some long string > splitting and collection. Try it out in the interpreter: Test data: >>> test = "'en' | 'the brown

Re: Splitting string into dictionary

2005-06-30 Thread George Sakkis
"David Pratt" > Thanks George! You guys are great! I am always learning. Python is > awesome!! Yeap, that was the reaction of many/most of us when we stumbled upon python. Welcome aboard ! George -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting string into dictionary

2005-06-30 Thread David Pratt
Thanks George! You guys are great! I am always learning. Python is awesome!! On Friday, July 1, 2005, at 02:33 AM, George Sakkis wrote: > "Robert Kern" wrote: > >> Ignore the last message. >> >> translations = [x.strip(" '") for x in line.split('|')] >> d = dict(zip(translations[::2], transla

Re: Splitting string into dictionary

2005-06-30 Thread George Sakkis
"David Pratt" wrote: > Wow Robert that is incredible python magic! I am trying to figure out > what this is doing since my attempts were regex and some long string > splitting and collection. > > Ok. So it is a list comprehension and then collection. What is zip > doing in the second line? > > R

Re: Splitting string into dictionary

2005-06-30 Thread David Pratt
Pretty amazing Devan! Great regex! Thank you. Regards, David On Friday, July 1, 2005, at 02:29 AM, Devan L wrote: > One line solution. > dict(re.findall(r"'(.+?)' \| '(.+?)'(?:\s\||$)",yourtexthere)) > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mai

Re: Splitting string into dictionary

2005-06-30 Thread David Pratt
Wow Robert that is incredible python magic! I am trying to figure out what this is doing since my attempts were regex and some long string splitting and collection. Ok. So it is a list comprehension and then collection. What is zip doing in the second line? Regards David On Friday, July 1,

Re: Splitting string into dictionary

2005-06-30 Thread George Sakkis
"Robert Kern" wrote: > Ignore the last message. > > translations = [x.strip(" '") for x in line.split('|')] > d = dict(zip(translations[::2], translations[1::2])) Or in case you're working with a lot and/or huge records, the second line can be more efficiently written as: from itertools import i

Re: Splitting string into dictionary

2005-06-30 Thread Devan L
One line solution. dict(re.findall(r"'(.+?)' \| '(.+?)'(?:\s\||$)",yourtexthere)) -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting string into dictionary

2005-06-30 Thread Robert Kern
David Pratt wrote: > I have string text with language text records that looks like this: > > 'en' | 'the brown cow' | 'fr' | 'la vache brun' > > Two or more language records can exist in each string (example above > shows 2 - but could contain more) > The second vertical line character in the ex

Re: Splitting string into dictionary

2005-06-30 Thread Robert Kern
David Pratt wrote: > I have string text with language text records that looks like this: > > 'en' | 'the brown cow' | 'fr' | 'la vache brun' > > Two or more language records can exist in each string (example above > shows 2 - but could contain more) > The second vertical line character in the ex

Splitting string into dictionary

2005-06-30 Thread David Pratt
I have string text with language text records that looks like this: 'en' | 'the brown cow' | 'fr' | 'la vache brun' Two or more language records can exist in each string (example above shows 2 - but could contain more) The second vertical line character in the example above is the record break