用字符串中的重复标记替换两个字符串之间的字符串

2024-05-29 11:04:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python,我有一个多行字符串,如下所示:

[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"]

我想用变量(newName)替换这些类型字符串中的"adamredsox24",但是从一个集合到另一个集合,子字符串并不总是"adamredsox24",而且对我来说是未知的。换句话说,白色后面的引号中的子字符串可以是任何东西,我不知道它是什么,但我想替换它,我该怎么做

此外,单词white将始终存在,我要替换的单词将始终位于单词white旁边的引号内,整行将始终包含在[]中

要查看数据示例,请参见以下代码片段:

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)

我尝试过以下方法:

newName="BOB"
stringBlock=re.sub('[White.*?"]','[White "' + newName + '"]',stringBlock, flags=re.DOTALL)

还有很多变化,但都没有运气。以下是gameID的几个值,以查看更多数据示例: P2xKlpp6,is3qBnmQ,kEdXQOIw


Tags: 数据字符串httpsorg示例单词引号white
2条回答

我不确定这是否行得通。参考

 import requests
    gameID = "nOA5FDoZ"
    r = requests.post(
    "https://lichess.org/games/export/_ids",
    data = gameID,
    params={"moves":"false", "opening":"true"}
    )
    l = r.text.splitlines()
    
    s1 = l[0].split('"')
    newInput = input("Enter new Event \n")
    s1[1] = newInput
    #print(s1[]
    listToStr = '"'.join(map(str, s1))
    l[0] = listToStr
    print(l)

我在API文档中做了一些研究,看来可靠地访问所需数据的最简单方法是以JSON格式获取数据

Link to API Docs

下面是请求JSON格式数据的代码示例:

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)

使用json获取响应允许您按名称轻松访问响应的每个部分,并根据需要操作数据

为白色棋子更换棋手

new_player = {
    "name": "NEWPLAYER",
    "id": "newplayer"
}

game_data['players']['white']['user'] = new_player

print(game_data['players']['white'])  

Json响应示例

{
    "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\"]\n\n1. 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-0\n\n\n",
    "clock": {
        "initial": 600,
        "increment": 0,
        "totalTime": 600
    }
}

相关问题 更多 >

    热门问题