Python 上一个链接的相关部分。还可以尝试在连接中使用_unicode=True,因为人们似乎认为这是必需的 for play_type in play_codes['general']: if play_type in play_tx:

Python 上一个链接的相关部分。还可以尝试在连接中使用_unicode=True,因为人们似乎认为这是必需的 for play_type in play_codes['general']: if play_type in play_tx: ,python,utf-8,Python,Utf 8,上一个链接的相关部分。还可以尝试在连接中使用_unicode=True,因为人们似乎认为这是必需的 for play_type in play_codes['general']: if play_type in play_tx: code = play_codes['general'][play_type]['code'] break play_codes = { 'general':{


上一个链接的相关部分。还可以尝试在连接中使用_unicode=True,因为人们似乎认为这是必需的
    for play_type in play_codes['general']:
        if play_type in play_tx:
            code = play_codes['general'][play_type]['code']
            break
play_codes = {
              'general':{
                         'falló en':            {'code':'',   'description':'generic out'},
                         'se ponchó':           {'code':'K',  'description':'strike out'},
                         'por base por bolas':  {'code':'BB', 'description':'walk'}
                        }
             }
TypeError: 'in <string>' requires string as left operand
# -*- coding: utf-8 -*-

play_codes = {
    'general':{
        'falló en':            {'code':'',   'description':'generic out'},
        'se ponchó':           {'code':'K',  'description':'strike out'},
        'por base por bolas':  {'code':'BB', 'description':'walk'}
    }
}

#play_tx = ('falló en', 'se ponchó', 'por base por bolas')
play_tx = "M.Brito falló en rolata al lanzador."

for play_type in play_codes['general']:
    if play_type in play_tx:
        code = play_codes['general'][play_type]['code']
        break
# -*- coding: utf-8 -*-

def db_connect():
  DBUSER = 'root'
  DBPASSWD = 'xxx'
  DB = 'cuba'

  try:
    db = MySQLdb.connect(user=DBUSER, passwd=DBPASSWD, db=DB, charset='utf8',     cursorclass=MySQLdb.cursors.DictCursor)
    cursor = db.cursor()
  except:
    print 'Cannot connect to database. Check credentials'
    raise SystemExit


def list_games():

  query = """SELECT
               game_id,
               season
             FROM cuba.games
             WHERE game_id <> 0
             ORDER BY game_id ASC"""
  cursor.execute(query)

  gamelist = []

  for rec in cursor.fetchall():
    gamelist.append(rec)

  return(gamelist)


def list_pbp(game_id):

  query = """SELECT
               game_id,
               event_id,
               inning_tx,
               play_tx,
               away_score_ct,
               home_score_ct
             FROM cuba.pbp
             WHERE game_id = %d
             ORDER BY event_id """ % game_id
  cursor.execute(query)

  pbplist = []

  for rec in cursor.fetchall():
    pbplist.append(rec)

  return(pbplist)


def main():

  play_codes = {
              'general':   {
                            'falló en':            {'code':'',   'h_cd':'0','event_cd':'2' ,'description':'generic out'},
                            'se ponchó':           {'code':'K',  'h_cd':'0','event_cd':'3' ,'description':'strike out'},
                            'por base por bolas':  {'code':'BB', 'h_cd':'0','event_cd':'14','description':'walk'},
                            'bolas intencional':   {'code':'IBB','h_cd':'0','event_cd':'15','description':'intentional walk'},
                            'se embasó por error': {'code':'E',  'h_cd':'0','event_cd':'18','description':'error'},
                            'bateó  rolata':       {'code':'FC', 'h_cd':'0','event_cd':'19','description':'fielders choice'},
                            'bateó sencillo':      {'code':'S',  'h_cd':'1','event_cd':'20','description':'single'},
                            'bateó doble':         {'code':'D',  'h_cd':'2','event_cd':'21','description':'double'},
                            'bateó triple':        {'code':'T',  'h_cd':'3','event_cd':'22','description':'triple'},
                            'bateó cuadrangular':  {'code':'HR/','h_cd':'4','event_cd':'23','description':'homerun'}
                           }
               }


  db_connect()
  gamelist = list_games()

  for game in gamelist:

    game_id = game['game_id']

    pbp = list_pbp(game_id)

    for play in pbp:
      play_tx = play['play_tx']

      code = ''

#      play_tx = 'R.Bordon bateó sencillo en rolata al izquierdo.'
      for play_type in play_codes['general']:

          if play_type in play_tx:
              code = play_codes['general'][play_type]['code']
              print code,play_type, play_tx
              break


  db_close()

if __name__ == '__main__':
    main()