Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:34:39+00:00 2026-06-15T03:34:39+00:00

I’m getting a keyerror exception when I input a player name here that is

  • 0

I’m getting a keyerror exception when I input a player name here that is not in the records list. I can search it and get back any valid name, but if I input anything else, i get a keyerror. I’m not really sure how to go about handling this since it’s kindof confusing already dealing with like 3 sets of data created from parsing my file.

I know this code is bad I’m new to python so please excuse the mess – also note that this is a sortof test file to get this functionality working, which I will then write into functions in my real main file. Kindof a testbed here, if that makes any sense.

This is what my data file, stats4.txt, has in it:

[00000] Cho'Gath - 12/16/3 - Loss - 2012-11-22
[00001] Fizz - 12/5/16 - Win - 2012-11-22
[00002] Caitlyn - 13/4/6 - Win - 2012-11-22
[00003] Sona - 4/5/9 - Loss - 2012-11-23
[00004] Sona - 2/1/20 - Win - 2012-11-23
[00005] Sona - 6/3/17 - Loss - 2012-11-23
[00006] Caitlyn - 14/2/16 - Win - 2012-11-24
[00007] Lux - 10/2/14 - Win - 2012-11-24
[00008] Sona - 8/1/22 - Win - 2012-11-27

Here’s my code:

import re

info = {}
records = []
search = []
with open('stats4.txt') as data:
    for line in data:
        gameid = [item.strip('[') for item in line.split(']')]
        del gameid[-1]
        gameidstr = ''.join(gameid)
        gameid = gameidstr
        line = line[7:]
        player, stats, outcome, date = [item.strip() for item in line.split('-', 3)]
        stats = dict(zip(('kills', 'deaths', 'assists'), map(int, stats.split('/'))))
        date = tuple(map(int, date.split('-')))
        info[player] = dict(zip(('gameid', 'player', 'stats', 'outcome', 'date'), (gameid, player, stats, outcome, date)))
        records.append(tuple((gameid, info[player])))

print "\n\n", info, "\n\n" #print the info dictionary just to see 
champ = raw_input() #get champion name
#print info[champ].get('stats').get('kills'), "\n\n"
#print "[%s] %s - %s/%s/%s - %s-%s-%s" % (info[champ].get('gameid'), champ, info[champ].get('stats').get('kills'), info[champ].get('stats').get('deaths'), info[champ].get('stats').get('assists'), info[champ].get('date')[0], info[champ].get('date')[1], info[champ].get('date')[2])
#print "\n\n"
#print info[champ].values()

i = 0
for item in records: #this prints out all records
    print "\n", "[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (records[i][0], records[i][1]['player'], records[i][1]['stats']['kills'], records[i][1]['stats']['deaths'], records[i][1]['stats']['assists'], records[i][1]['outcome'], records[i][1]['date'][0], records[i][1]['date'][1], records[i][1]['date'][2])
    i = i + 1

print "\n" + "*" * 50
i = 0
for item in records:
    if champ in records[i][1]['player']:
        search.append(records[i][1])
    else:
        pass
    i = i + 1

s = 0

if not search:
    print "no availble records" #how can I get this to print even if nothing is inputted in raw_input above for champ?


print "****"
for item in search:
        print "\n[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (search[s]['gameid'], search[s]['player'], search[s]['stats']['kills'], search[s]['stats']['deaths'], search[s]['stats']['assists'], search[s]['outcome'], search[s]['date'][0], search[s]['date'][1], search[s]['date'][2])
        s = s + 1

I tried setting up a Try; Except sort of thing but I couldn’t get any different result when entering an invalid player name. I think I could probably set something up with a function and returning different things if the name is present or not but I think I’ve just gotten myself a bit confused. Also notice that no match does indeed print for the 8 records that aren’t matches, though thats not quite how I want it to work. Basically I need to get something like that for any invalid input name, not just a valid input that happens to not be in a record in the loop.

Valid input names for this data are:
Cho’Gath, Fizz, Caitlyn, Sona, or Lux – anything else gives a keyerror, thats what I need to handle so it doesn’t raise an error and instead just prints something like “no records available for that champion” (and prints that only once, rather then 8 times)

Thanks for any help!

[edit] I was finally able to update this code in the post (thank you martineau for getting it added in, for some reason backticks aren’t working to block code and it was showing up as bold normal text when i pasted. Anyways, look at if not search, how can I get that to print even if nothing is entered at all? just pressing return on raw_input, currently it prints all records after **** even though i didn’t give it any search champ

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:34:42+00:00Added an answer on June 15, 2026 at 3:34 am

    Here’s some modifications that I think address your problem. I also reformatted the code to make it a little more readable. In Python it’s possible to continue long lines onto the next either by ending with a \ or just going to the next line if there’s an unpaired ‘(‘ or ‘[‘ on the previous line.

    Also, the way I put code in my questions or answer here is by cutting it out of my text editor and then pasting it into the edit window, after that I make sure it’s all selected and then just use the {} tool at the top of edit window to format it all.

    import re
    from pprint import pprint
    
    info = {}
    records = []
    with open('stats4.txt') as data:
        for line in data:
            gameid = [item.strip('[') for item in line.split(']')]
            del gameid[-1]
            gameidstr = ''.join(gameid)
            gameid = gameidstr
            line = line[7:]
            player, stats, outcome, date = [item.strip() for item in line.split('-', 3)]
            stats = dict(zip(('kills', 'deaths', 'assists'), map(int, stats.split('/'))))
            date = tuple(map(int, date.split('-')))
            info[player] = dict(zip(('gameid', 'player', 'stats', 'outcome', 'date'),
                                    (gameid, player, stats, outcome, date)))
            records.append(tuple((gameid, info[player])))
    
    #print "\n\n", info, "\n\n" #print the info dictionary just to see
    pprint(info)
    champ = raw_input("Champ's name: ") #get champion name
    #print info[champ].get('stats').get('kills'), "\n\n"
    #print "[%s] %s - %s/%s/%s - %s-%s-%s" % (
    #       info[champ].get('gameid'), champ, info[champ].get('stats').get('kills'),
    #       info[champ].get('stats').get('deaths'), info[champ].get('stats').get('assists'),
    #       info[champ].get('date')[0], info[champ].get('date')[1],
    #       info[champ].get('date')[2])
    #print "\n\n"
    #print info[champ].values()
    
    i = 0
    for item in records: #this prints out all records
        print "\n", "[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (
                     records[i][0], records[i][1]['player'], records[i][1]['stats']['kills'],
                     records[i][1]['stats']['deaths'], records[i][1]['stats']['assists'],
                     records[i][1]['outcome'], records[i][1]['date'][0],
                     records[i][1]['date'][1], records[i][1]['date'][2])
        i = i + 1
    
    print "\n" + "*" * 50
    i = 0
    search = []
    for item in records:
        if champ in records[i][1]['player']:
            search.append(records[i][1])
        i = i + 1
    if not search:
        print "no match"
        exit()
    
    s = 0
    for item in search:
            print "\n[%s] %s - %s/%s/%s - %s - %s-%s-%s" % (search[s]['gameid'],
                  search[s]['player'], search[s]['stats']['kills'],
                  search[s]['stats']['deaths'], search[s]['stats']['assists'],
                  search[s]['outcome'], search[s]['date'][0], search[s]['date'][1],
                  search[s]['date'][2])
            s = s + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.