SolebaySharp

Stack Overflow - Extract parts of a .txt file into a list using Python

Jul 30th, 2020 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.10 KB | None | 0 0
  1. #Continuation of troubleshooting from my question on Stack overflow.
  2.  
  3. # @MisterNox I really appreciate the time you have put into helping thus far. Here is my code.
  4.  
  5. #import JSON
  6. import json
  7.  
  8. #I define the txt2 path
  9. txt2 = '/Users/myname/Environments/my_env/Learning Python/Riot API/test_run2'
  10.  
  11. #Open it so it can be read, as it needs to be read as JSON strings
  12. with open("/Users/myname/Environments/my_env/Learning Python/Riot API/test_run2", "r") as f:
  13.     txt2 = f.read()
  14.  
  15. #I implement your solution
  16. txt = json.loads(txt2.replace("'", '"'))
  17. output = [x["gameId"] for x in txt["matches"]]
  18. print(output)
  19.  
  20.  
  21. #___________________________________________________________
  22.  
  23. #Contents of test_run2
  24.  
  25. #This is EXACTLY what is in test_run2 as I am only using your JSON example. If I can get YOUR example to work I will mark the question as correct, because I know I can learn a bit more about JSON and easily get mine to work.
  26.  
  27.  
  28. """{'matches': [{'platformId': 'EUW1', 'gameId': 4728220480, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1595790394190, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4727511119, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595776780604, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4727366101, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1595774913709, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4724700944, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595632344294, 'role': 'DUO_SUPPORT', 'lane': 'NONE'}, {'platformId': 'EUW1', 'gameId': 4724662512, 'champion': 163, 'queue': 420, 'season': 13, 'timestamp': 1595629227345, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4724497303, 'champion': 203, 'queue': 420, 'season': 13, 'timestamp': 1595627065458, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4722652071, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595530253160, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4709654115, 'champion': 875, 'queue': 420, 'season': 13, 'timestamp': 1594854382037, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4709546735, 'champion': 113, 'queue': 420, 'season': 13, 'timestamp': 1594850792558, 'role': 'DUO_SUPPORT', 'lane': 'TOP'}, {'platformId': 'EUW1', 'gameId': 4706883023, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1594723475884, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703718275, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1594551535462, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703411360, 'champion': 245, 'queue': 420, 'season': 13, 'timestamp': 1594509687989, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703403465, 'champion': 245, 'queue': 420, 'season': 13, 'timestamp': 1594506333259, 'role': 'NONE', 'lane': 'JUNGLE'}]}"""
  29.  
  30. #_______________________________________________
  31.  
  32. #And yes, this (your initial response) runs perfectly for me.
  33.  
  34. txt2 = """{'matches': [{'platformId': 'EUW1', 'gameId': 4728220480, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1595790394190, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4727511119, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595776780604, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4727366101, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1595774913709, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4724700944, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595632344294, 'role': 'DUO_SUPPORT', 'lane': 'NONE'}, {'platformId': 'EUW1', 'gameId': 4724662512, 'champion': 163, 'queue': 420, 'season': 13, 'timestamp': 1595629227345, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4724497303, 'champion': 203, 'queue': 420, 'season': 13, 'timestamp': 1595627065458, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4722652071, 'champion': 104, 'queue': 420, 'season': 13, 'timestamp': 1595530253160, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4709654115, 'champion': 875, 'queue': 420, 'season': 13, 'timestamp': 1594854382037, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4709546735, 'champion': 113, 'queue': 420, 'season': 13, 'timestamp': 1594850792558, 'role': 'DUO_SUPPORT', 'lane': 'TOP'}, {'platformId': 'EUW1', 'gameId': 4706883023, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1594723475884, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703718275, 'champion': 121, 'queue': 420, 'season': 13, 'timestamp': 1594551535462, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703411360, 'champion': 245, 'queue': 420, 'season': 13, 'timestamp': 1594509687989, 'role': 'NONE', 'lane': 'JUNGLE'}, {'platformId': 'EUW1', 'gameId': 4703403465, 'champion': 245, 'queue': 420, 'season': 13, 'timestamp': 1594506333259, 'role': 'NONE', 'lane': 'JUNGLE'}]}"""
  35.  
  36.  
  37.  
  38. txt = json.loads(txt2.replace("'", '"'))
  39. output = [x["gameId"] for x in txt["matches"]]
  40. print(output)
Add Comment
Please, Sign In to add comment