2013-05-17 44 views
-13
add_numbers("A1", "Element 560234 65952 6598881 20203256 2165883 659562 654981 24120 261240 31648948 23900 5512400 5512900 5612400 5612900") 

add_numbers("A2", "Element 261240 31659 5612400 76803256 3165883 659863 654224 44120 261240 31648948 23900 3612200 9512900 5612400 5642924") 

add_numbers("A3", "Element 841225 65952 2165883 63103256 2165883 644861 344966 84120 161540 31653948 23900 5513426 5518906 5682405 8682932") 

我希望得到一個字典(從上面這是一個txt文件中的字符串),看起來像這樣:Python的 - 字符串分割線

{A1: 560234, 65952,6598881, 20203256,2165883, 659562,....} 

{A2: 261240 31659 5612400,....} 

{A3: 841225 65952 2165883,....} 

你有什麼想法?我怎樣才能做到這一點?謝謝。

+5

請問你能格式化你的代碼嗎? –

+0

什麼是add_numbers? – njzk2

+2

基本上,這將是一個string.split()的問題[1:] – njzk2

回答

4

的理解是,要處理這個

add_numbers("A1", "Element 560234 65952 6598881 20203256 2165883 659562 654981 24120 261240 31648948 23900 5512400 5512900 5612400 5612900") 

add_numbers("A2", "Element 261240 31659 5612400 76803256 3165883 659863 654224 44120 261240 31648948 23900 3612200 9512900 5612400 5642924") 

add_numbers("A3", "Element 841225 65952 2165883 63103256 2165883 644861 344966 84120 161540 31653948 23900 5513426 5518906 5682405 8682932") 

爲文本文件的文字內容轉換成字典,我會做這樣的:

import re // import regular expression module 
d = {} 

for line in open("myfile.txt", "r"): 
    if not line.strip(): continue  // Skip blank lines 
    data = re.findall('"([^"]*)"', line) // Extract text between double quotes 

    if len(data) != 2: continue   // There were not exactly two pairs of double quotes, skip this line 

    key, value = data 
    d[key] = map(int, value.split()[1:]) // Remove "Element" and convert numbers to integers, add to dictionary 

An expl正則表達式"([^"]*)"的anation:

  • "()"引號
  • [^"]*的0或多個字符的字符串,是不是"

re.findall將顯示在列表中返回結果裏面的東西匹配。

編輯

I get an error. ValueError: need more than 1 value to unpack

您必須不包含在文件中的兩個對雙引號的線。我更新了上面的代碼以忽略不符合您的規範的行。

+1

@Joran雖然好,但,我更新了我對'map(int,...)'結果的回答+1 – HennyH

+0

我得到一個錯誤ValueError:需要多個值才能解包 – tchike

+0

@tchike添加了一個編輯來響應此 –

4
import re,ast 
def add_numbers(d,key,elements): #we pass in a reference to a dict, which we update 
    d[key] = map(int,elements.split()[1:]) #Returns ["Element",...], so we select all but first [1:] 
dic = {} 
with open('file.txt') as f: 
    for line in f: 
     key,elems = ast.literal_eval(re.search(r'\((.+)\)',line).group(0)) 
     add_numbers(dic,key,elems) 

現在生產

>>> 
{'A1': [560234, 65952, 6598881, 20203256, 2165883, 659562, 654981, 24120, 261240, 31648948, 23900, 5512400, 5512900, 5612400, 5612900], 'A3': [841225, 65952, 2165883, 63103256, 2165883, 644861, 344966, 84120, 161540, 31653948, 23900, 5513426, 5518906, 5682405, 8682932], 'A2': [261240, 31659, 5612400, 76803256, 3165883, 659863, 654224, 44120, 261240, 31648948, 23900, 3612200, 9512900, 5612400, 5642924]} 
+0

我不能使用這個,因爲add_numbers(「A1」,「元素56023 ....是在一個txt文件中,我 – tchike

+0

@tchike我會更新我的解決方案,如果你發佈這樣的文件內容的樣本 – HennyH

+0

txt文件的內容是我的問題的第一部分 – tchike