By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Splitting string into dictionary

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
One line solution.
dict(re.findall(r"'(.+?)' \| '(.+?)'(?:\s\||$)",yourtexthere))

Jul 19 '05 #2

P: n/a
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.