2017-07-24 81 views
0

我想从足球推文中提取角的数量。不同的用户以不同的方式提及数字。子串处理和字符串提取

我不是很强大的python字符串处理,但试图编写逻辑提取corners值。如果有人告诉我是否写了任何错误的逻辑,或者考虑所有情况的更好方法,我真的很感激。

# I get wrong corner number if it is two digit value 
def getCorners(self, text): 
    # print text 
    try: 
     if 'corner' in text: 
      print text 
      corner = "Yes"     
      #To check the case: digit+ corner (it should be mentioned as over <digit> corners) 
      if str(text.find('+ corner')-1).isdigit(): 
       corner_pos = text.find('+ corner')-1 
       nos_corners = text[corner_pos] 
       corner = str(nos_corners) + ' corners' 
       # if text.find('+ corners') == (corner_pos-10): 
       if '+corner' in text: 
        corner = "over " + str(nos_corners) + 'corners' 
      elif str(text.find(' corner')-1).isdigit(): 
       corner_pos = text.find('+ corner')-1 
       nos_corners = text[corner_pos] 
       corner = str(nos_corners) + ' corners' 
      print corner + "\n" 
     else: 
      corner = "None"   
    except: 
     pass 

我仍然在试图改善逻辑,但任何建议,以改善逻辑或大加赞赏鸣叫

@SkyBet coquelin and xhaka to be booked. Benteke to score, Arsenal to win over 10 match corners #RequestABet 
@SkyBet under 2.5 goals, arsenal -3 handicap on corners, palace 30+ booking points, dann carded #RequestABet 
@SkyBet can u price on Arsenal -1, Sanchez to score, 4+ corners each team, 20+ booking points each team & woodwork to be hit #RequestABet 
@SkyBet Arsenal to win by 2+ goals, Sanchez to score, 40+ booking points, 10+ match corners #RequestABet 
@SkyBet #RequestABet BTTS, over 2.5 goals, 11+ match corners, ward and xhaka to be carded 
@SkyBet Kane to score, over 1.5 goals, Watford 20+ booking points, Tottenham 4+ corners #RequestABet 
#RequestABet Goal and 4 corners in each half of televised games. Also, 10+corners and 30+booking points each in televised games. 
Think you need to #it as #RequestABet . 10+ corners and 30+ bookings in the 3 televised games 
@skybet #RequestABet Holebas, Wimmer & Ambrabat to be carded, 10+ match corners and over 2.5 match goals 
@SkyBet Son & Eriksen to score, 4+ Watford corners, 8+ Tottenham corners, 30+ Watford booking pts #RequestABet 
@SkyBet Benteke to score, monreal to be booked, over ten corners and a draw #RequestABet #arsenalpalace 
@SkyBet arsenal & Spurs both to score 2+ goals & take 5+ corners 5+ corners in each half Watford/Spurs & arsenal/palace #RequestABet 
BTTS Over 2.5 goals 4 corners each team and 20+ booking points each team #RequestABet @SkyBet 
BTTS Over 2.5 goals 4 corners each half and 20+ booking points each team #RequestABet @SkyBet 
@SkyBet #RequestABet Watford v Spurs - Tottenham to win, 6+ Tottenham corners, 40+ match booking points 
@SkyBet - Deeney to score, BTTS, 6+ Tottenham corners, Holebas to be carded #RequestABet 
@SkyBet Could I request a price on #RequestABet Tottenham to win, over 2.5 goals , 9+ match corners, 30+ match booking points 
@skybet Tottenham to win, over 2.5 goals and Tottenham 7+ corners. #requestabet 
@SkyBet Kane score, Spurs win, 9+ corners, Watford 20+ booking points #RequestABet 
@SkyBet #RequestABet - Tottenham to win, Tottenham to have 5+ corners and 30+ match booking points. 
@SkyBet palace to win, btts, arsenal 6+ corners, kelly and monreal carded #RequestABet 
@SkyBet giroud to score, btts, palace +1 handicap, arsenal 6+ corners, monreal and kelly carded #RequestABet 
@SkyBet #RequestABet Tottenham 8+ match corners, Watford 30+ booking points 
@SkyBet Could I request a price on #RequestABet for Watford v Tottenham Over 1.5 goals , 9+ match corners, 20+ match booking points 
7+ goals 20+corners and 10+cards across both of today's premier league games #RequestABet 
@SkyBet #RequestABet 5+ corners in each half of the Watford & Arsenal games? 
@SkyBet #RequestABet Kane and Eriksen to score, Tottenham to win, 30+ booking points, 8+ Tottenham corners 
@SkyBet #RequestABet Tottenham to score 2+ take 4+ corners and 30+ match booking points and 

回答

1

我希望这会帮助你的

类型一些Python功能。

loadData = open("file.txt", "r") 
loadData = loadData.readlines() 


def check(data): 
    for i in range(0, len(data)): 
     if data[i] == 'corners' and len(data[i - 1]) < 3: 
      print(data[i - 1]) 


for data in loadData: 
    dataSplitted = data.split() 
    check(dataSplitted) 

而结果将是:

4+ 
4+ 
4 
5+ 
5+ 
4 
4 
5+ 
5+ 
4+ 
+0

尼斯逻辑,谢谢。你可以添加你的逻辑' +比赛角落案件。我的尝试是添加'elif data [i] =='corner'和len(data [i - 2])<3: print(data [i - 2])''。它会起作用吗? – user3449212