【问题标题】:Splitting date range of dates into two to lists将日期的日期范围分成两个列表
【发布时间】:2020-10-19 00:27:26
【问题描述】:

我要吐槽以下日期列表:

Month=['1 October 2020 to 31 October 2020',
        '1 October 2020 to 31 October 2020',
        '1 October 2020 to 31 October 2020',
        '1 October 2020 to 31 October 2020',
        '1 October 2020 to 31 October 2020']

想要的输出如下:

 Month = [['1 October 2020', '31 October 2020'],
        ['1 October 2020', '31 October 2020'],
        ['1 October 2020','31 October 2020'],
        ['1 October 2020', '31 October 2020'],
        ['1 October 2020','31 October 2020']]

如何使用正则表达式来做到这一点。

我使用了Month.str.split('to'),但这不能正常工作,因为十月包含to,结果将十月分成三个字符串。因此,我想我必须为此使用regex。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: python regex string list split


    【解决方案1】:

    使用' to ' 作为分区,而不是仅仅使用to——这无论如何都会更好地匹配输入格式,因为如果您在to 上拆分,您还需要去除空格。

    >>> [list(i.split(' to ') for i in Month)]
    [[['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020']]]
    

    【讨论】:

      猜你喜欢
      • 2019-08-25
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多