utf8字符在" if ... in"测试

时间:2014-11-29 00:43:03

标签: python utf-8

我有这段代码

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

引用字典'play_codes'(下面转载的部分)

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_type,如果匹配则将“代码”分配给代码(基本上是if then elseif循环)

它工作得很漂亮 - 除非play_type包含utf8扩展字符,例如代字号,然后我得到:

TypeError: 'in <string>' requires string as left operand

在此之后,我要解析韩语,所以这是我需要掌握的东西!

2 个答案:

答案 0 :(得分:0)

此代码适用于我。请注意顶部的文件编码声明。这可能是你的问题(即它违反了ascii)。

# -*- 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

如果结果是问题那么你可以在这里看到更多信息 https://www.python.org/dev/peps/pep-0263/

嗯,即使我将编码设置为ascii也能正常工作。所以现在我有点困惑。发布所有代码。

答案 1 :(得分:0)

这是一套完整的代码

# -*- 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()