-2

I have a pgn file with multiple games. I want to separate all the games into different files or just one text file so that I can separate all the White moves and Black moves. Any help?

Kapil Gund
  • 19
  • 2

1 Answers1

1

Assuming your file looks like this:

[Event "Llyods Bank"] [...]

1.e4 [...]

[Event "Lloyds Bank"] [...]

1.e4 [...]

Then, a simple approach would be to append to the list all the lines until you find the 2nd '\n'

Here's a naive piece of code:

from os import read


my_file = open("Adams.pgn", "r")
content_list = my_file. readlines()
print(content_list)

list_games = []
game = []
j = 0
for i in content_list:
    if i == "\n":
        j += 1
    if j == 2:
        j = 0
        list_games.append(game)
        game = []
    game.append(i)
if game.empty() == False:
    list_games.append(game)

for i in game:
    print(game)
Achille G
  • 748
  • 6
  • 19
  • We get this error when performing any normal read, write operations UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1891: invalid start byte – Kapil Gund May 18 '21 at 13:01
  • Try this: my_file = open("Adams.pgn", "r", errors="ignore") Please give us a glimpse of what your file looks like – Achille G May 18 '21 at 13:14