【问题标题】:Find, reformat and replace multiple dates in string查找、重新格式化和替换字符串中的多个日期
【发布时间】:2017-12-22 14:25:10
【问题描述】:

我有一个包含日期的长字符串,想更新所有日期的格式。

以下是我写的以及我无法弄清楚的伪代码:

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

谢谢

【问题讨论】:

  • 您可以尝试使用正则表达式来匹配 dd/mm/YYYY 和字典来将数字 mm 值映射到相应的字符串表示形式。但是,您导入的datetime 库中可能有一些内容,或者查看pandas。我已经对此进行了一些日期时间操作,如果您发现相关内容,科学图书馆总是会提供很多支持。编辑:见stackoverflow.com/questions/3276180/…@unutbu 在该线程上的帖子提到了一些可能对你有用的东西
  • 谢谢@DarrelHolt 我会看看pandas 我在datetimedateutil 之间折腾。但喜欢datetime 让你构建自己的格式的方式。
  • 我确实看到了这个问题,但不幸的是dateutil.parser 只能处理一个日期的字符串。我的字符串将有 0-n 个日期。

标签: python


【解决方案1】:

第一个建议不是一个完整的解决方案,请跳到下面的第一个编辑部分

如果您想通过几种方式调整您的代码,您可以这样做。首先将字符串分解成碎片:

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()))
...

【讨论】:

  • 这种方法能否很好地扩展?我只是好奇,因为它实际上将line 填充为来自txt 文件的行,可能有几千行。
  • @Jake 我用一些工作代码进行了编辑。这个使用了名为“re”的正则表达式库,并且您可以路由通过“sub”函数找到的匹配项(用于替代)。通过我对您的功能和使用正则表达式库所做的小修改,您应该能够处理相当大的文件。
  • @Jake 如果需要,您可以一次重写整个文件,请参阅我所做的第二次编辑。我在一个由您的示例制成的文件上对其进行了测试,该文件在 ~25000 行上一遍又一遍地重复,这没问题(~1-2 秒)。
  • 哇,谢谢。太棒了!对 txt 文件(每个约 20,000 行)很有魅力,而且速度非常快
  • @Jake 乐于助人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多