Skip to content

Instantly share code, notes, and snippets.

@FLAK-ZOSO
Created January 26, 2022 09:45
Show Gist options
  • Save FLAK-ZOSO/74054e070899035bb2187f1c10ca2008 to your computer and use it in GitHub Desktop.
Save FLAK-ZOSO/74054e070899035bb2187f1c10ca2008 to your computer and use it in GitHub Desktop.
with open('file.txt', 'r') as file:
lines = file.read().split('\n') # lines is a list of the lines of the file
dictionary: dict = {}
for index, line in enumerate(lines):
line = line.split(' ')
dictionary[str(index)] = {
"white": line[0],
"black": line[1],
"white score": line[2],
"black score": line[4]
}
print(dictionary)
fischer morphy 1 - 0
carlsen alekhine 0 - 1
kasparov capablanca 1/2 - 1/2
@FLAK-ZOSO
Copy link
Author

@hauzan2112, I'm happy to have helped you. In this case you can accept my answer on Stack Overflow since it's a working one.

@hauzan2112
Copy link

checkmarked. cant upvote yet

@FLAK-ZOSO
Copy link
Author

Thank you! If you need help and you don't want to ask on Stack Overflow, you can always find me here 🙂

@hauzan2112
Copy link

hauzan2112 commented Jan 26, 2022

well, i do

in my old code with dictionary as:

match_result = [ # dict name
    {
        "match": "1",           # match
        "wht_ply": "fischer",   # white player name
        "blck_ply": "morphy",   # black player name
        "wht_scr":"1",          # white player score
        "dash":"-",             # unused dash
        "blck_scr":"0"          # black player score
    },

i use these code to find match with 1/2-1/2 result and print exact match:

     for item in match_result:
      if item["wht_scr"] and item["blck_scr"] == "1/2":
          print("match",item["match"],"game between white player",item["wht_ply"],"and black player",item["blck_ply"],"ended up in draw")

but when i change the item name(like "wht_scr" to "white score" to the one you use in your code it says

line 14, in <module>
    if item["white socre"] and item["black score"] == "1/2":
TypeError: string indices must be integers
for item in match_result:
   if item["white score"] and item["black score"] == "1/2":
       print("game between white player", item["white"], "and black player", item["black"], "ended up in draw")

@FLAK-ZOSO
Copy link
Author

FLAK-ZOSO commented Jan 26, 2022

Yes, that's because I've organized the dictionary in a different way. Let me explain:

{
    "1": {
        "white": "Jack",
        "black": "Mary",
        "white score": "1",
        "black score": "0"
    }
    "2": {
        // Some other stuff
    }
}

So, if you want to access the score, you have to do this way:

for item in matches.keys(): # matches is the big dictionary
   if matches[item]["white score"] and matches[item]["black score"] == "1/2":
       # Do your stuff

@hauzan2112
Copy link

been answered

for items in dictionary.values():
    if items["white score"] and items["black score"] == "1/2":
        print("game between white player,", items["white"], "and black player,", items["black"], "ended up in draw")

@FLAK-ZOSO
Copy link
Author

Yes, does it work?

@hauzan2112
Copy link

it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment