469,646 Members | 1,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Splitting string into dictionary

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 in the pattern (between 'cow' and 'fr')

What is the shortest route to getting this into a dictionary like:

{'en':'the brown cow','fr':'la vache brun'}

The language code is always 2 lower case letters.

Many thanks.
Jul 19 '05 #1
2 1624
One line solution.
dict(re.findall(r"'(.+?)' \| '(.+?)'(?:\s\||$)",yourtexthere))

Jul 19 '05 #2
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 with the noun in both number and gender. But it's a
long time since I was in school. Are adjectives unisex these days?
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by sbucking | last post: by
7 posts views Thread by qwweeeit | last post: by
17 posts views Thread by Qiangning Hong | last post: by
2 posts views Thread by CharChabil | last post: by
11 posts views Thread by Peter Kirk | last post: by
2 posts views Thread by shadow_ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.