2017-07-17 31 views
1

我有一個很長的字符串包含日期,並希望更新所有日期的格式。查找,重新格式化並替換字符串中的多個日期

以下是我與位的僞代碼我想不通一起寫入:

import datetime 

current_date_format = "%d/%m/%Y" 
new_date_format = "%d/%b/%Y" 

def main(): 
    line = "This is text dated 01/02/2017, and there are a few more dates such as 03/07/2017 and 09/06/2000" 
    print(line) 
    # Best way to pull out and replace all of the dates? 
    # pseudo: 
    for each current_date_format in line as date_in_line 
     temp_date = fix_date(date_in_line) 
     line.replace(date_in_line, temp_date) 
    print(line) 

def fix_date(date_string=''): 
    return datetime.datetime.strptime(date_string, current_date_format).strftime(new_date_format) 

在這種情況下,如果要打印:

This is text dated 01/02/2017, and there are a few more dates such as 03/07/2017 and 09/06/2000 
This is text dated 01/FEB/2017, and there are a few more dates such as 03/JUL/2017 and 09/JUN/2000 

感謝

+0

您可以嘗試使用正則表達式來匹配'dd/mm/YYYY'和一個字典,以將數值'mm'值映射到相應的字符串表示形式。不過,您可能已在導入的'datetime'庫中找到了一些東西,或者可能會看看'pandas'。我已經完成了一些日期時間操作,如果你發現相關的東西,科學圖書館總是有很多支持。 編輯:請參閱https://stackoverflow.com/questions/3276180/extracting-date-from-a-string-in-python @unutbu在該帖子中發佈的帖子提及了可能對您有用的內容 –

+0

謝謝@DarrelHolt我會看看'pandas'我在'datetime'和'dateutil'之間徘徊。但喜歡'datetime'的方式讓你建立自己的格式。 – Jake

+0

我確實看到了這個問題,但不幸的是'dateutil.parser'只能處理一個日期的字符串。我的字符串將有0-n個日期。 – Jake

回答

3

第一建議不是一個完整的解決方案,請跳至下面的第一個編輯部分

如果你想用幾種方式調整你的代碼,你可以這樣做。最初只是打出來的字符串成片:

line = "This is text dated 01/02/2017, and there are a few more dates such as 03/07/2017 and 09/06/2000" 
words = line.split() # by default it splits on whitespace 

現在,您可以與您的各項輸入的發揮。然後,您可以嘗試使用您的fix_date方法來解析您的日期,並重新編譯字符串:

updated_line = '' 
for word in words: 
    try: 
     updated_line += fix_date(word) + ' ' 
    except: 
     updated_line += word + ' ' 
updated_line = updated_line[:-1] # gets rid of the extra trailing space 
print(updated_line) 

編輯:跑步時我意識到這個具有安裝日期標點符號的問題。我正在再次傳球。

下面是一些工作代碼:

import datetime 
import re 

current_date_format = "%d/%m/%Y" 
new_date_format = "%d/%b/%Y" 

def main(): 
    line = "This is text dated 01/02/2017, and there are a few more dates such as 03/07/2017 and 09/06/2000" 
    print(line) 
    line = re.sub(r'\d{2}/\d{2}/\d{4}',fix_date,line) 
    print(line) 

def fix_date(rem): 
    date_string = rem.group() 
    return datetime.datetime.strptime(date_string, current_date_format).strftime(new_date_format) 

main() 

編輯2:作爲正則表達式的方法適用於龐大的字符串儘可能小的,如果你的文件尺寸很小,足以載入一下子就可以只是做了一個鏡頭:

import datetime 
import re 

current_date_format = "%d/%m/%Y" 
new_date_format = "%d/%b/%Y" 

def main(): 
    with open('my_file.txt','r') as f: 
     text = f.read() 
    with open('my_fixed_file.txt','w') as f: 
     f.write(re.sub(r'\d{2}/\d{2}/\d{4}',fix_date,text)) 

def fix_date(rem): 
    date_string = rem.group() 
    return datetime.datetime.strptime(date_string, current_date_format).strftime(new_date_format) 

main() 

甚至更​​緊湊,通過調整文件讀取/寫入部:

... 
with open('my_file.txt','r') as f: 
    with open('my_fixed_file.txt','w') as f2: 
     f2.write(re.sub(r'\d{2}/\d{2}/\d{4}',fix_date,f.read())) 
... 
+0

這個方法可以很好地擴展嗎?我只是好奇,因爲它實際上將'line'作爲來自'txt'文件的行來填充,這些文件可能長達幾千行。 – Jake

+0

@Jake我用一些工作代碼做了編輯。這個使用名爲're'的正則表達式庫,並且可以路由通過'sub'函數找到的匹配(替代)。通過我對你的函數進行的小編輯以及正則表達式庫的使用,你應該能夠處理相當大的文件。 –

+0

@Jake如果您想要,您可以一次重寫整個文件,請參閱我所做的第二個編輯。我在一個由你的例子製作的文件上對〜25000行進行了重複測試,這是沒有問題的(約1-2秒)。 –