通过匹配字符从字符串中获取子字符串

时间:2017-11-26 18:26:20

标签: python

我正在尝试从我拥有的字符串列表中提取价格。

这是PriceList的示例:

PriceList = ['Weekly:$3,600.00\n\nMonthly:$15,120.00',
             'Daily:$3,000.00\n\nWeekly:$8,400.00\n\nMonthly:$33,600.00',
             'Daily:$1,800.00\n\nWeekend:$2200.00',
             'Day:$3,600.00\n\nWeekly:$6,000.00\n\nMonthly:$24,000.00']

PriceList开始,我想提取MonthlyPriceDailyPrice

如果MonthlyPrice不存在则为NA

我试过这段代码:

for item in PriceList:
    if item.find("Monthly")!= -1:
        MonthlyPrice = (item[8:])
    if item.find("Yearly")!= -1:
        YearlyPrice = (item[7:])
    if item.find("Weekly")!= -1:
        WeeklyPrice = (item[7:])

但它不起作用,请帮助我。

预期结果

对于列表中的第一个示例,

print(WeeklyPrice)将为您提供$3600.00
列表中第一个示例的print(DailyPrice)将为您提供 - NA

4 个答案:

答案 0 :(得分:2)

不是笨拙地解析包含在列表中的那个孤独的字符串,而是构建一个字典。

>>> prices = ['Daily:$3,000.00\n\nWeekly:$8,400.00\n\nMonthly:$33,600.00']
>>> prices = dict(time_price.split(':') for time_price in prices[0].split())
>>> prices
{'Monthly': '$33,600.00', 'Daily': '$3,000.00', 'Weekly': '$8,400.00'}

从这里开始,孩子可以提取你想要的任何东西。

>>> prices['Daily']
'$3,000.00'
>>> prices.get('NotFound', 'NA')
'NA'

请注意,如果您打算在将来将价格转换为float值时进行任何算术,那么这是一个不错的主意。

>>> prices = {k:float(v[1:].replace(',', '')) for k,v in prices.items()}
>>> prices
{'Monthly': 33600.0, 'Daily': 3000.0, 'Weekly': 8400.0}

答案 1 :(得分:1)

试试这个:

for item in PriceList:
    for line in item.splitlines():
        if line.startswith('Daily:'):
            DailyPrice = line[len('Daily:'):]
        if line.startswith('Weekly:'):
            WeeklyPrice = line[len('Weekly:'):]
        if line.startswith('Monthly:'):
            MonthlyPrice = line[len('Monthly:'):]

答案 2 :(得分:1)

具有regex.findall()功能和特定正则表达式模式的完整解决方案:

import re

price_list = ['Weekly:$3,600.00\n\nMonthly:$15,120.00',
              'Daily:$3,000.00\n\nWeekly:$8,400.00\n\nMonthly:$33,600.00',
              'Daily:$1,800.00\n\nWeekend:$2200.00',
              'Day:$3,600.00\n\nWeekly:$6,000.00\n\nMonthly:$24,000.00']

base_dict = {'Yearly': 'NA', 'Monthly': 'NA', 'Weekly': 'NA', 'Daily': 'NA'}
pat = re.compile('(?:(Yearly|Monthly|Weekly|Daily):(\$[\d.,]+))')

for p_str in price_list:
    result_d = dict(base_dict)
    result_d.update(pat.findall(p_str))
    print('Yearly: {Yearly}, Monthly: {Monthly}, Weekly: {Weekly}, Daily: {Daily}'.format(**result_d))

输出:

Yearly: NA, Monthly: $15,120.00, Weekly: $3,600.00, Daily: NA
Yearly: NA, Monthly: $33,600.00, Weekly: $8,400.00, Daily: $3,000.00
Yearly: NA, Monthly: NA, Weekly: NA, Daily: $1,800.00
Yearly: NA, Monthly: $24,000.00, Weekly: $6,000.00, Daily: NA

答案 3 :(得分:1)

您可以这样做,首先从prices中的每个数据字符串创建一个临时PriceList字典,然后使用字典get()方法确定特定值是否在字符串。查找字典中的内容非常快,因此这种实现方法也非常有效。

PriceList = ['Weekly:$3,600.00\n\nMonthly:$15,120.00',
             'Daily:$3,000.00\n\nWeekly:$8,400.00\n\nMonthly:$33,600.00',
             'Daily:$1,800.00\n\nWeekend:$2200.00',
             'Day:$3,600.00\n\nWeekly:$6,000.00\n\nMonthly:$24,000.00']

for data in PriceList:
    prices = dict(item.split(':') for item in data.split('\n\n'))
    MonthlyPrice = prices.get("Monthly", "NA")
    YearlyPrice = prices.get("Yearly", "NA")
    WeeklyPrice = prices.get("Weekly", "NA")
    print('MonthlyPrice: {:<10}, YearlyPrice: {:<10}, WeeklyPrice: {:<10}'.format(
            MonthlyPrice, YearlyPrice, WeeklyPrice))

输出:

MonthlyPrice: $15,120.00, YearlyPrice: NA        , WeeklyPrice: $3,600.00
MonthlyPrice: $33,600.00, YearlyPrice: NA        , WeeklyPrice: $8,400.00
MonthlyPrice: NA        , YearlyPrice: NA        , WeeklyPrice: NA
MonthlyPrice: $24,000.00, YearlyPrice: NA        , WeeklyPrice: $6,000.00