【问题标题】:Split String with Multiple Dates拆分具有多个日期的字符串
【发布时间】:2020-03-25 21:49:28
【问题描述】:

我正在尝试创建一个在一个或多个日期拆分字符串的函数

{0: 'Stabilizing (P1 events)(11/5/19) -3.8.4\xa0released',
 1: '(9/20/19)\-3.8.3 released; Enabled 100% for \xa0(released the APK to the store, not the actual app itself)',
 2: '(11/12/19) Accepted & Released. Enabled 100% for.',
 3: '(6/11/19) app released with feature flag disabled(9/11/19)\100% on  for P1 events',
 4: '(6/11/19) app released with feature flag disabled(9/11/19)\100% on for P1 events',
 5: '(1/6/20)\xa0Released with feature flag disabled & LiveChannel bug fixed.\xa0(Live Channels disabled)(2/13/20)\xa0FireTv\xa0100% on for P1 events'}

例如,上例中的字符串 #3 将被拆分为:

['(6/11/19)', 'app released with feature flag disabled', '(9/11/19)', '100% on for P1 events']

我创建了另一个从每个字符串中提取日期的函数

def extract_comments_date(date):
    val  = re.findall('(\d+/\d+/\d+)', date)
    if len(val) == 1:
        return f"({val[0]})"
    else:
        return [f"({i})" for i in val]
extract_comments_date(mydict[3])
output: ['(6/11/19)', '(9/11/19)']

但现在我想知道如何在存储在列表中的多个分隔符(日期)上拆分字符串。

【问题讨论】:

  • 尝试拆分:\(\d{1,2}/\d{1,2}/\d{2,4}\)

标签: python regex date split


【解决方案1】:

只是一个粗略的例子或深思熟虑(抱歉,眼皮合上!)...

import re
import pandas as pd

pattern = re.compile(r'(\(\d{1,2}\/\d{1,2}\/\d{1,2}\))')

d = {0: 'Stabilizing (P1 events)(11/5/19) -3.8.4\xa0released',
 1: '(9/20/19)\-3.8.3 released; Enabled 100% for \xa0(released the APK to the store, not the actual app itself)',
 2: '(11/12/19) Accepted & Released. Enabled 100% for.',
 3: '(6/11/19) app released with feature flag disabled(9/11/19)\100% on  for P1 events',
 4: '(6/11/19) app released with feature flag disabled(9/11/19)\100% on for P1 events',
 5: '(1/6/20)\xa0Released with feature flag disabled & LiveChannel bug fixed.\xa0(Live Channels disabled)(2/13/20)\xa0FireTv\xa0100% on for P1 events'}

for key, value in d.items():
    splitting = pattern.sub(r'|\1|', value).split('|')
    splitting = [i for i in splitting if i] 
    print(splitting)

输出:

['Stabilizing (P1 events)', '(11/5/19)', ' -3.8.4\xa0released']
['(9/20/19)', '\\-3.8.3 released; Enabled 100% for \xa0(released the APK to the store, not the actual app itself)']
['(11/12/19)', ' Accepted & Released. Enabled 100% for.']
['(6/11/19)', ' app released with feature flag disabled', '(9/11/19)', '@% on  for P1 events']
['(6/11/19)', ' app released with feature flag disabled', '(9/11/19)', '@% on for P1 events']
['(1/6/20)', '\xa0Released with feature flag disabled & LiveChannel bug fixed.\xa0(Live Channels disabled)', '(2/13/20)', '\xa0FireTv\xa0100% on for P1 events']

更新:由于字符串中有逗号,因此可能会在 | 上拆分。反而?无论如何,某些字符不在字符串中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多