3

次のような文字列 s があるとします。

s = 'Title: A title Date: November 23 1234 Other: Other information'

次のような辞書を作成することは可能ですか:

{'Title':'A title','Date':'November 23 1234','Other':'Other information'}

最初は単純にコロンのある場所で分割するだけだと思っていましたが、タイトルの値が何であるかわからないため、タイトル自体にコロンが含まれている可能性があります。残念ながら、この情報のソースもコンマで区切られていないので、それも面倒です. EG、どうすればそれを行うことができますか:

s = 'Title: Example: of a title Date: November 23 1234 Other: Other information'

その例のタイトルは ですExample: of a title

この質問を確認しましたが、私の質問に対する回答ではありませんでした。

前もって感謝します。

4

3 に答える 3

3
import re
from itertools import izip

s = 'Title: Example: of a title Date: November 23 1234 Other: Other information'

keys = ['Title', 'Date', 'Other']
pattern = re.compile('({})\s+'.format(':|'.join(keys)))

print dict(izip(*[(i.strip() for i in (pattern.split(s)) if i)]*2))

アウト:

{'Date:': 'November 23 1234 ',
 'Other:': 'Other information',
 'Title:': 'Example: of a title '}
于 2013-03-18T07:29:35.857 に答える
1

あなたは正規表現でそれを行うことができます:

>>> import re
>>> 
>>> s = 'Title: A title Date: November 23 1234 Other: Other information'
>>> matches = re.findall(r'(\w+): ((?:\w+\s)+)', s)
>>> 
>>> dict(matches)
    {'Date': 'November 23 1234 ', 'Other': 'Other ', 'Title': 'A title '}
于 2013-03-18T07:05:52.343 に答える
0

コロンが複数ある(ネストされている可能性がある)ため、コロンで分割することはできません。

キーワード(、、Title)が修正されている場合はDateOther次の正規表現を試すことができます。

import re
reg_ex = re.compile("Title\:(.+)Date\:(.+)Other\:(.+)")
reg_ex.match(s).groups() #(' A title ', ' November 23 1234 ', ' Other information')
于 2013-03-18T07:21:59.133 に答える