2012-01-09 96 views
0

我正在嘗試編寫一個函數,該函數能夠解析出包含一組響應的已定義消息的文件,但在如何執行此操作時正處於失敗狀態。作爲配置解析聊天消息

例如配置文件將如下:

[Message 1] 
1: Hey 
    How are you? 
2: Good, today is a good day. 
3: What do you have planned? 
    Anything special? 
4: I am busy working, so nothing in particular. 
    My calendar is full. 

而不前述它被認爲是應答的一部分,只是在談話而不等待響應的另一消息的號碼。每個新的一行。

感謝

編輯:配置文件將包含多條消息,我想有隨機從所有這些選擇的能力。也許將來自對話的每個回覆作爲列表存儲,然後帶有額外消息的回覆可以攜帶換行符,然後僅通過換行符分隔它們。我不確定什麼是最好的操作。

更新:

我已經得到了大部分這種編碼創辦至今:

def parseMessages(filename): 
    messages = {} 
    begin_message = lambda x: re.match(r'^(\d)\: (.+)', x) 
    with open(filename) as f: 
    for line in f: 
     m = re.match(r'^\[(.+)\]$', line) 
     if m: 
     index = m.group(1) 
     elif begin_message(line): 
     begin = begin_message(line).group(2) 
     else: 
     cont = line.strip() 
    else: 
     # ?? 
    return messages 

但現在我停留於能夠將它們存儲到字典的方式,我倒是想..

我將如何得到這個存儲的字典,如:

{'Message 1': 
    {'1': 'How are you?\nHow are you?', 
    '2': 'Good, today is a good day.', 
    '3': 'What do you have planned?\nAnything special?', 
    '4': 'I am busy working, so nothing in particular.\nMy calendar is full' 
    } 
} 

或者如果任何人有更好的主意,我願意提供建議。

再一次,謝謝。

更新兩個

這是我的最終代碼:

import re 
def parseMessages(filename): 
    all_messages = {} 
    num = None 
    begin_message = lambda x: re.match(r'^(\d)\: (.+)', x) 
    with open(filename) as f: 
    messages = {} 
    message = [] 
    for line in f: 
     m = re.match(r'^\[(.+)\]$', line) 
     if m: 
     index = m.group(1) 
     elif begin_message(line): 
     if num: 
      messages.update({num: '\n'.join(message)}) 
     all_messages.update({index: messages}) 
     del message[:] 
     num = int(begin_message(line).group(1)) 
     begin = begin_message(line).group(2) 
     message.append(begin) 
     else: 
     cont = line.strip() 
     if cont: 
      message.append(cont) 
    return all_messages 

回答

1

聽起來並不太難。幾乎Python的僞代碼:

for line in configFile: 
    strip comments from line 
    if line looks like a section separator: 
     section = matched section 
    elsif line looks like the beginning of a reply: 
     append line to replies[section] 
    else: 
     append line to last reply in replies[section][-1] 

您可能需要使用re模塊的「樣子」操作。 :)

+0

Gotcha。我會嘗試使用您的psuedocode作爲指導進行編碼。我有一些想法會按照你的建議遵循。 – mikeyy 2012-01-09 18:17:55

0

如果您的字符串數量相對較少,爲什麼不直接在dict中將它們作爲字符串文本提供?

{'How are you?' : 'Good, today is a good day.'}