I using python and I have a multi-line string like this:
[Event "2nd Elite Classical Team Battle"] [Site "https://lichess.org/is3qBnmQ"] [Date "2020.07.25"] [White "adamredsox24"] [Black "ricjimenez_50"] [Result "0-1"] [UTCDate "2020.07.25"]
I want to replace "adamredsox24"
in a large set of these types of strings with a variable (newName
) but from set to set the substring will not always be "adamredsox24"
at all and will be unknown to me. In other words, the substring in the quotes after the word White could be anything and I have no idea what it is but I want to replace it, how would I do that?
Also, the word white will always be there and the word I’m looking to replace will always be inside quotes beside the word White and the entire line will always be enclosed in [ ].
To see a sample of the data here is a code snippet:
import requests gameID = "nOA5FDoZ" r = requests.post( "https://lichess.org/games/export/_ids", data = gameID, params={"moves":"false", "opening":"true"} ) stringBlock = r.text print(stringBlock)
I have tried things like:
newName="BOB" stringBlock=re.sub('[White.*?"]','[White "' + newName + '"]',stringBlock, flags=re.DOTALL)
And many variations of that with no luck. Here are a few more values for gameID to see more examples of the data: P2xKlpp6, is3qBnmQ, kEdXQOIw
Advertisement
Answer
I did some research in the API documentation and it appears the easiest way for you to reliably access the data you want is to get the data back in JSON.
Here is an example of the code that requests data in JSON format:
import requests gameID = "nOA5FDoZ" r = requests.post( "https://lichess.org/games/export/_ids", data=gameID, params={"moves": "true", "opening": "true"}, headers={"Accept": "application/x-ndjson"} # tell the api to send JSON ) # Save the json response game_data = r.json() # Access the chess game players player_white = game_data['players']['white'] player_black = game_data['players']['black'] print(player_white) print(player_black)
Getting the response in json allows you to easily access each part of the response by name and manipulate the data as you see fit.
Changing the player for the white chess piece
new_player = { "name": "NEWPLAYER", "id": "newplayer" } game_data['players']['white']['user'] = new_player print(game_data['players']['white'])
Json Response example
{ "id": "nOA5FDoZ", "rated": true, "variant": "standard", "speed": "rapid", "perf": "rapid", "createdAt": 1577505410981, "lastMoveAt": 1577506071611, "status": "resign", "players": { "white": { "user": { "name": "UMAR1738", "id": "umar1738" }, "rating": 1881, "ratingDiff": 13 }, "black": { "user": { "name": "keshavkaurav", "id": "keshavkaurav" }, "rating": 2029, "ratingDiff": -9 } }, "winner": "white", "opening": { "eco": "B56", "name": "Sicilian Defense: Classical Variation", "ply": 10 }, "moves": "e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 Nc6 Be3 e6 f3 Be7 Be2 O-O Qd2 a6 O-O-O d5 exd5 Nxd5 Nxd5 Qxd5 Nb3 Qxd2+ Rxd2 Nb4 a3 Nd5 Bc5 Bg5 Bxf8 Bxd2+ Kxd2 Kxf8 Rd1 Nf4 Bf1 e5 g3 Ng6 Ke3 Be6 Nc5 Ke7 Nxe6 Kxe6 Bc4+ Ke7 Bd5 Rd8 c3 Kf6 Rd3 b6 Bc4 Rxd3+ Bxd3 a5 b4 axb4 cxb4 Ke6 a4 Kd6 a5 bxa5 bxa5 Kc5 a6 Kb6 h4 f6 h5 Nf8 Ke4 g6 hxg6 hxg6 Kd5 Nd7 Kd6 Nc5 Bxg6 Nxa6 Ke6 Kc5 Kxf6 Kd6 g4 Nc7 Bf7 Nb5 g5 Nd4 g6 Nxf3 g7 e4 g8=Q e3 Qb8+", "pgn": "[Event "Rated Rapid game"]n[Site "https://lichess.org/nOA5FDoZ"]n[Date "2019.12.28"]n[White "UMAR1738"]n[Black "keshavkaurav"]n[Result "1-0"]n[UTCDate "2019.12.28"]n[UTCTime "03:56:50"]n[WhiteElo "1881"]n[BlackElo "2029"]n[WhiteRatingDiff "+13"]n[BlackRatingDiff "-9"]n[Variant "Standard"]n[TimeControl "600+0"]n[ECO "B56"]n[Opening "Sicilian Defense: Classical Variation"]n[Termination "Normal"]nn1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 Nc6 6. Be3 e6 7. f3 Be7 8. Be2 O-O 9. Qd2 a6 10. O-O-O d5 11. exd5 Nxd5 12. Nxd5 Qxd5 13. Nb3 Qxd2+ 14. Rxd2 Nb4 15. a3 Nd5 16. Bc5 Bg5 17. Bxf8 Bxd2+ 18. Kxd2 Kxf8 19. Rd1 Nf4 20. Bf1 e5 21. g3 Ng6 22. Ke3 Be6 23. Nc5 Ke7 24. Nxe6 Kxe6 25. Bc4+ Ke7 26. Bd5 Rd8 27. c3 Kf6 28. Rd3 b6 29. Bc4 Rxd3+ 30. Bxd3 a5 31. b4 axb4 32. cxb4 Ke6 33. a4 Kd6 34. a5 bxa5 35. bxa5 Kc5 36. a6 Kb6 37. h4 f6 38. h5 Nf8 39. Ke4 g6 40. hxg6 hxg6 41. Kd5 Nd7 42. Kd6 Nc5 43. Bxg6 Nxa6 44. Ke6 Kc5 45. Kxf6 Kd6 46. g4 Nc7 47. Bf7 Nb5 48. g5 Nd4 49. g6 Nxf3 50. g7 e4 51. g8=Q e3 52. Qb8+ 1-0nnn", "clock": { "initial": 600, "increment": 0, "totalTime": 600 } }