Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass from tiddlywiki list to python list

Tiddlywiki uses internally a space-separated tags for making a list of tags. But it uses [[ and ]] to limit multi-word tags. That is, a list of foo, ram doo, bar and very cool becomes in tiddlywiki a string like that:

"foo [[ram doo]] bar [[very cool]]"

How can I transform that into python list that look like:

['foo', 'ram doo', 'bar', 'very cool']

"foo [[ram doo]] bar".split() does not work for me..

like image 905
somenxavier Avatar asked Jan 12 '18 12:01

somenxavier


2 Answers

With regex:

import re
a = "foo [[ram doo]] bar [[very cool]] something else"
pattern = re.compile(r'\[\[[^\]]+\]\]|[^\[\] ]+')
print([i.strip(' []') for i in pattern.findall(a)])

Prints ['foo', 'ram doo', 'bar', 'very cool', 'something', 'else']

Regex basically "tokenizes" the string (borders are either [[..]] or space, in that order), the list comprehension then removes the brackets from the tokens.

like image 108
hansaplast Avatar answered Nov 12 '22 02:11

hansaplast


A simple regular expression works:

>>> import re
>>> [x.strip() for x in re.split('\[\[|\]\]',  "foo [[ram doo]] bar [[very cool]]") if x]
['foo', 'ram doo', 'bar', 'very cool']
like image 3
Mike Müller Avatar answered Nov 12 '22 02:11

Mike Müller