如何re.sub()我不想要的字符串?

时间:2018-03-31 21:24:43

标签: python regex

我有这个:

var1 = {u'data-sport_id': u'basket_us', u'data-event_id':     u'sacramento_kings__golden_state_warriors', u'data-compet_id': u'nba_-_matchs', u'title': u'Basket US - NBA - Matchs - Sacramento Kings // Golden State Warriors - 1 avril 2018 - 4h10', u'data-name': u'sportif.clic.accueil.zone_centrale.details', u'href': u'/event/523450/sports/basket-us/nba-matchs/golden-state-warriors-chez-les-sacramento-kings', u'data-placement': u'bottom', u'data-toggle': u'tooltip', u'class': [u'trow--event', u'tc-track-element-events']}
var2 = {u'data-sport_id': u'rugby_a_xiii', u'data-event_id': u'st_georgeilla_dragons__newcastle_knights', u'data-compet_id': u'nrl', u'title': u'Rugby \xe0 XIII - NRL - St George/Illa Dragons // Newcastle Knights - 1 avril 2018 - 8h10', u'data-name': u'sportif.clic.accueil.zone_centrale.details', u'href': u'/event/521076/sports/rugby-%C3%A0-xiii/nrl/st-georgeilla-dragons-newcastle-knights', u'data-placement': u'bottom', u'data-toggle': u'tooltip', u'class': [u'trow--event', u'tc-track-element-events']}

我想保留:

var1 = basket_us
var2 = rugby_a_xiii

我正在尝试这个但是它不起作用:

re.findall(r"data-sport_id':(.)*'(.)*'", var1)
re.findall(r"data-sport_id':(.)*'(.)*'", var2)

但我想知道,我可以做一个re.sub()并且只保留我想要的东西吗?

(.)*data-sport_id(.)*[WHAT I WANT]'(.)*

1 个答案:

答案 0 :(得分:2)

这些是dicts,您可以像以下一样访问它们:

代码:

var1.get('data-sport_id', '')

或者如果您确定该密钥将出现如下:

var1['data-sport_id']

测试代码:

var1 = {u'data-sport_id': u'basket_us',
        u'data-event_id': u'sacramento_kings__golden_state_warriors',
        u'data-compet_id': u'nba_-_matchs',
        u'title': u'Basket US - NBA - Matchs - Sacramento Kings // Golden State Warriors - 1 avril 2018 - 4h10',
        u'data-name': u'sportif.clic.accueil.zone_centrale.details',
        u'href': u'/event/523450/sports/basket-us/nba-matchs/golden-state-warriors-chez-les-sacramento-kings',
        u'data-placement': u'bottom', u'data-toggle': u'tooltip',
        u'class': [u'trow--event', u'tc-track-element-events']}
var2 = {u'data-sport_id': u'rugby_a_xiii',
        u'data-event_id': u'st_georgeilla_dragons__newcastle_knights',
        u'data-compet_id': u'nrl',
        u'title': u'Rugby \xe0 XIII - NRL - St George/Illa Dragons // Newcastle Knights - 1 avril 2018 - 8h10',
        u'data-name': u'sportif.clic.accueil.zone_centrale.details',
        u'href': u'/event/521076/sports/rugby-%C3%A0-xiii/nrl/st-georgeilla-dragons-newcastle-knights',
        u'data-placement': u'bottom', u'data-toggle': u'tooltip',
        u'class': [u'trow--event', u'tc-track-element-events']}


print(var1.get('data-sport_id', ''))
print(var2.get('data-sport_id', ''))

结果:

basket_us
rugby_a_xiii