2013-03-18 76 views
2

字符串字典比方說,有一個字符串,S,它看起來像這樣:創建與冒號

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

是否有可能創造這將是一本字典:

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

在首先,我只是簡單地把它分成冒號的地方,但不知道標題的價值是什麼,標題本身可能有冒號。唉這個信息的來源並沒有用逗號分隔它,所以這也是一個痛苦。 E.G,你怎麼能這麼做:

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

那個標題在那個例子是Example: of a title

我已選中 this question,但它沒有回答我的問題。

在此先感謝。

+0

會不會有前面的單詞之間的任何間隙冒號和冒號本身?如果沒有,您可以使用自定義正則表達式來查找那些特殊的令牌並在那裏分割。 – 2013-03-18 07:05:21

+0

@SudiptaChatterjee不會有。我對正則表達式並不是非常有經驗(即我知道沒有什麼:p),所以我不認爲我能夠自己做到這一點。 – TerryA 2013-03-18 07:07:28

+2

您是否事先知道密鑰(例如標題,日期,其他)? – root 2013-03-18 07:17:22

回答

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 '} 
+0

美麗。謝謝。 – TerryA 2013-03-18 07:40:00

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 '} 
+0

感謝您的快速回答,但「日期」似乎在標題值中,並且Date的值似乎被忽略 – TerryA 2013-03-18 07:08:49

+0

@Haidro:我想它確實消失了。這可能是一個更好的方法,但正則表達式適用於您的示例。 – Blender 2013-03-18 07:11:59

0

你不能僅僅用冒號分割它,因爲有他們的倍數(可能嵌套)。

如果關鍵字(TitleDateOther)是固定的,你可以嘗試以下的正則表達式:

import re 
reg_ex = re.compile("Title\:(.+)Date\:(.+)Other\:(.+)") 
reg_ex.match(s).groups() #(' A title ', ' November 23 1234 ', ' Other information')