Splitting string into dictionary

D

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 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.
 
D

Devan L

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

John Machin

David said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top