我試圖在一組有些不規則的數據,看起來像執行字符串分割:字符串分割上新的生產線,標籤和若干空格
\n\tName: John Smith
\n\t Home: Anytown USA
\n\t Phone: 555-555-555
\n\t Other Home: Somewhere Else
\n\t Notes: Other data
\n\tName: Jane Smith
\n\t Misc: Data with spaces
我想這個轉換成一個元組/字典,我後來將分裂在冒號:
,但首先我需要擺脫所有額外的空白。我猜一個正則表達式是最好的方法,但我似乎無法得到一個有效的,下面是我的嘗試。
data_string.split('\n\t *')
奇妙的是,[List comprehension](http://docs.python.org/tutorial/datastructures.html#list-comprehensions)的語法並不是我以前見過的,所以我認爲我'你必須閱讀它。 – PopeJohnPaulII
工作就像一個魅力!真棒!謝謝 –