2012-04-13 69 views
0

代替邏輯在Python中我需要爲以下情形的邏輯我使用分割功能這一點。 我有包含輸入的字符串,如下所示。查找和Python中

「ID674021384 25/01/1986 heloo嗨,感謝5分鐘和25-01-1988。」

「ID909900000 25-01-1986你好10分鐘。」

並輸出應爲如下所示的替換日期格式爲「日」和時間格式「時間」。

「ID674021384日期您好嗨,感謝時間日期。」

「ID909900000日期Hello時間。」

而且也是我需要的日期和時間的每個ID下面

ID674021384 DATE計數爲顯示:2時間:1

ID909900000日期:1周時間:1

+0

你試圖使用're'模塊;常用表達? – Surya 2012-04-13 14:14:43

+0

使用正確定義的正則表達式來處理您的輸入。如果你能顯示你有什麼代碼,這將有所幫助,否則給一些更多的示例內容。 – cfedermann 2012-04-13 13:37:44

+0

這是從聊天中獲取數據的新要求..我還沒有開始編碼..它看起來像這樣輸入聊天 「ID674021384 \t 25/01/1986謝謝你應該在大約0分鐘內連接到代理」 而放出來應該是 「ID674021384 \t日期謝謝您應該連接到代理約時間」 ID674021384日期:1周時間:1 – 2012-04-13 14:19:54

回答

2
>>> import re 
>>> from collections import defaultdict 
>>> lines = ["ID674021384 25/01/1986 heloo hi thanks 5 minutes and 25-01-1988.", "ID909900000 25-01-1986 hello 10 minutes."] 
>>> pattern = '(?P<date>\d{1,2}[/-]\d{1,2}[/-]\d{4})|(?P<time>\d+ minutes)' 
>>> num_occurences = {line:defaultdict(int) for line in lines} 
>>> def repl(matchobj): 
     num_occurences[matchobj.string][matchobj.lastgroup] += 1 
     return matchobj.lastgroup 

>>> for line in lines: 
     text_id = line.split(' ')[0] 
     new_text = re.sub(pattern,repl,line)  
     print new_text 
     print '{0} DATE:{1[date]} Time:{1[time]}'.format(text_id, num_occurences[line]) 
     print '' 


ID674021384 date heloo hi thanks time and date. 
ID674021384 DATE:2 Time:1 

ID909900000 date hello time. 
ID909900000 DATE:1 Time:1 
+0

謝謝jamylak ..你可以請我分享我的python教程,我們可以在python中發展我們的技能。我想學習python。這是我的Id:[email protected] – 2012-04-13 14:57:59

+0

沒問題。不確定哪些教程有很多不同的資源,您可以根據自己的技能來查找。這個問題之前已經在這個網站上提出過,所以你可以查看它。 – jamylak 2012-04-13 15:03:58

1

對於解析相似的文本行,如日誌文件,我經常使用正則表達式使用re模塊。雖然split()將工作做好也用於分離不包含空格和日期的部分領域,使用正則表達式可以讓你也確保格式匹配你所期望的,如果需要的話警告你一個怪異的輸入線。

使用正則表達式,您可以獲取日期和時間的各個字段,並從中構建datedatetime對象(均來自datetime模塊)。一旦你有這些對象,你可以將它們與其他類似的對象進行比較,並寫入新的條目,並根據需要設置日期的格式。我會推薦解析整個輸入文件(假設你正在讀取一個文件)並寫一個全新的輸出文件,而不是試圖改變它。

至於跟蹤的日期和時間計數,當你輸入不是太大,使用字典通常是這樣做的最簡單的方法。當你遇到一個具有特定ID的行時,在你的字典中找到與這個ID相對應的條目,如果沒有,就添加一個新的ID。這個條目本身可以是一個使用日期和時間作爲鍵的字典,其值是每個遇到的數量。

我希望這個答案將引導您的方式來解決,即使它不包含任何代碼。

+0

除非這個問題被標記爲'homework',代碼通常預期的那樣一個答案... – jamylak 2012-04-13 13:45:13

+1

@jamylak:無代碼的答案是一個代碼和研究免費問題的可接受的迴應。 – 2012-04-13 13:47:10

+0

好點...:D – jamylak 2012-04-13 13:47:46

0

你可以用一對夫婦正則表達式:

import re 

txt = 'ID674021384 25/01/1986 heloo hi thanks 5 minutes and 25-01-1988.' 

retime = re.compile('([0-9]+) *minutes') 
redate = re.compile('([0-9]+[/-][0-9]+[/-][0-9]{4})') 

# find all dates in 'txt' 
dates = redate.findall(txt) 
print dates 

# find all times in 'txt' 
times = retime.findall(txt) 
print times 

# replace dates and times in orignal string: 
newtxt = txt 
for adate in dates: 
    newtxt = newtxt.replace(adate, 'date') 

for atime in times: 
    newtxt = newtxt.replace(atime, 'time') 

輸出看起來是這樣的:

Original string: 
ID674021384 25/01/1986 heloo hi thanks 5 minutes and 25-01-1988. 
Found dates:['25/01/1986', '25-01-1988'] 
Found times: ['5'] 

New string: 
ID674021384 date heloo hi thanks time minutes and date. 

Dates and times found: 
ID674021384 DATE:2 TIME:1 

克里斯

+0

當我運行這個I不要得到那個輸出... – jamylak 2012-04-13 15:01:40