Get data from replay file with Python?

I’m sure there must be a way to do this, but I’ve never had to look at files in very much detail in Python so I’m not sure. Is there a way of getting the data in downloaded replay files into Python? For example, I would like to know how to get the position of units on a specific turn, but any help would be apprecited.

The replay file is formatted the same as the commands sent between the engine and your algo. Open the replay file and then load it using json.

If you need examples you can look at the code I wrote in the contributions folder of the starter-kit. Specifically look at the Replay class and the load_data method inside that class. There you can see it essentially does this (pseudo-code):

  1. Open the file you want using Python’s open().
  2. Use JSON to get the data using json.loads(<string>) as a dictionary.
  3. Do whatever you want with the data (stored as a dictionary). In my case I simply store it in the Replay class.

I had tried this beforehand, however it didn’t work

file = open(locations[0],“r”)
file = json.loads(

Which gives the error

Traceback (most recent call last):
File “C:/Users/dnw13/Desktop/C1GamesStarterKit-master/C1GamesStarterKit-master/Sample replays/”, line 15, in
File “C:/Users/dnw13/Desktop/C1GamesStarterKit-master/C1GamesStarterKit-master/Sample replays/”, line 11, in analyze
file = json.loads(
File “C:\Users\dnw13\AppData\Local\Programs\Python\Python37-32\lib\”, line 348, in loads
return _default_decoder.decode(s)
File “C:\Users\dnw13\AppData\Local\Programs\Python\Python37-32\lib\json\”, line 340, in decode
raise JSONDecodeError(“Extra data”, s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1927)

The replay file is not JSON in full. Rather, each individual line is in JSON format. So loop through the file line by line and then use JSON on that line. Here is a small example:

with open('replay.replay') as file:
    for line in file:
        line = line.strip()
        if line != '':
            data = json.loads(line)
1 Like

Thank you, that works :+1:

1 Like