2016-07-16 25 views
0

こんにちは私は次のコードを持っていますが、csv.readerが空であるためループは機能しません。 csvデータを含むファイルが正しく開きます。Python 3 csv.reader空の応答

理解のために: varポケモンは、任意のポケモン名を文字列とすることができます。 ボット、ロガー、イベントはハングアウトボットからのものです。 必要なライブラリがすべて読み込まれています。

コード:で

def pkmn_translate(bot, event, pokemon): 
    logger.info("translating pokemon name") 
    url = "https://raw.githubusercontent.com/PokeAPI/pokeapi/master/data/v2/csv/pokemon_species_names.csv" 
    request = urllib.request.Request(url, headers = {"User-agent":"Mozilla/5.0", "Accept-Charset":"utf-8"}) 
    try: 
     data = urllib.request.urlopen(request) 
     csv_data = data.read() 
     csvstr = str(csv_data).strip("b'") 
     lines = csvstr.split("\\n") 
     f = open('{}/pokemon_species_names.csv'.format(os.path.dirname(os.path.realpath(__file__))), "w",encoding='utf8') 
     for line in lines: 
      f.write(line + "\n") 
     f.close() 
     logger.info("translating db saved") 
    except urllib.error.URLError as e: 
     logger.info("{}: Error: {}".format(event.user.full_name, json.loads(e.read().decode("utf8","ignore"))['detail'])) 
     yield from bot.coro_send_message(event.conv, "{}: Error: {}".format(event.user.full_name, json.loads(e.read().decode("utf8","ignore"))['detail'])) 
     return 
    pokemon_id = "default" 

    f = open('{}/pokemon_species_names.csv'.format(os.path.dirname(os.path.realpath(__file__))), 'r', encoding='utf8') # opens the csv file 
    try: 
     logger.info("DEBUG: openFile") 

     #Quick and dirty fix because CSV File is very big 
     maxInt = sys.maxsize 
     decrement = True 

     while decrement: 
      # decrease the maxInt value by factor 10 
      # as long as the OverflowError occurs. 

      decrement = False 
      try: 
       csv.field_size_limit(maxInt) 
      except OverflowError: 
       maxInt = int(maxInt/10) 
       decrement = True 
     logger.info("DEBUG: maxInt = {}".format(maxInt)) 

     reader = csv.reader(f) 
     rows = list(reader) 
     for row in reader: 
      logger.info("DEBUG: row = {}".format(row)) 
      for column in row: 
       if pokemon == column: 
        #DEBUG 
        logger.info("Info: row = {}".format(row)) 
        #SET VAR 
        pokemon_id = rows[row][0] 
        #DEBUG 
        logger.info("Info: {}".format(pokemon_id)) 
        bot.coro_send_message(event.conv, "Info: {}".format(pokemon_id)) 
       else: 
        logger.info("Error: Name not in File!") 
        bot.coro_send_message(event.conv, "Error: Name not in File!") 
      else: 
       logger.info("DEBUG: Loop exited") 
     else: 
      logger.info("DEBUG: Loop exited") 
    except: 
     logger.info("Debug: Some error") 
    finally: 
     f.close()  # closing 
    logger.info("Debug func: PokemonID = {}".format(pokemon_id)) 
    yield from pokemon_id 
    return pokemon_id 

ループのために、それは読者の変数にデータがないし、それが失敗しました。私はcsv.readerを動作させる方法を知らない。 PS:私はpythonで全然騒がしいです。

+0

によって

reader = csv.reader(f) rows = list(reader) for row in reader: 

を交換してください。また、 'pokemon_id = rows [row] [0]'はあまり意味がないようです。 –

+0

'pokemon_id = rows [row] [0]'は後で修正しなければならないxD私はそれがまだ動作していないことを知っていますが、 – MTRNord

答えて

1

あなたのlist(reader)コールはリーダを消費しますが、これはforループでは空です。

答えによって、読者がすでに消費されるだけで

reader = csv.reader(f) 
    rows = list(reader) 
    for row in rows: