1

Hello I got the following code but the loop won't work because the csv.reader is empty. The file with the csv data is opened correctly.

For Understanding: var pokemon can be any pokemon name as string. bot, logger and event are vars comming from the Hangoutsbot. All needed libaries are loaded.

Code:

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

at the for loop it has no data in the reader variable and it fails. I don't know how to get the csv.reader to work. PS: I am an total noob at python.

2
  • As by the answer, the reader is already consumed. Also, pokemon_id = rows[row][0] does not seem to make much sense. Commented Jul 16, 2016 at 21:06
  • the pokemon_id = rows[row][0] has to be fixed later xD I know that that isn't working yet but thats because rows where at first something other ;) Commented Jul 16, 2016 at 21:12

1 Answer 1

2

your list(reader) call consumes the reader, which is empty on the for loop.

just replace

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

by

    reader = csv.reader(f)
    rows = list(reader)
    for row in rows:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.