谁能帮我用正则表达式来处理以下示例?

2024-05-18 08:44:50 发布

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

样本数据:

"PlayerId":169193,"PlayerName":"Alexandre Lacazette","PlaysOnHomeTeam":true,"OptaId":"p59966","GoalsConcededInBox":1,"ShotsInsideBox":8,"ShotsWoodwork":1,"TotalPasses":31,"PassSuccess":23,"KeyPasses":5,"DribblesAttempted":3,"DribblesWon":2,"AerialsWon":5,"AerialsLost":2,"TacklesSuccess":3,"TacklesAttempted":3

我想要的是一个包含引号内数据的列表,即 玩家ID 玩家名称 重头戏主队 奥普泰德 .. .. .. 你知道吗

有谁能告诉我正则表达式可以实现这一点吗


Tags: 数据true玩家样本alexandreplayeridplayernameshotsinsidebox
2条回答

您可以使用:

,?"(\w+)":

说明:

,?是说它需要,中的0或1

在句子前面。你知道吗

(\w+)是你想要的。它是单词字符a-zA-Z0-9_

在句子后面"

example

如果您只需要每对的左边字符串:

import json
s = '{'+ '''"PlayerId":169193,"PlayerName":"Alexandre Lacazette","PlaysOnHomeTeam":true,"OptaId":"p59966","GoalsConcededInBox":1,"ShotsInsideBox":8,"ShotsWoodwork":1,"TotalPasses":31,"PassSuccess":23,"KeyPasses":5,"DribblesAttempted":3,"DribblesWon":2,"AerialsWon":5,"AerialsLost":2,"TacklesSuccess":3,"TacklesAttempted":3''' + '}'
list(json.loads(s))

输出:

['PlayerId', 'PlayerName', 'PlaysOnHomeTeam', 'OptaId', 'GoalsConcededInBox', 'ShotsInsideBox', 'ShotsWoodwork', 'TotalPasses', 'PassSuccess', 'KeyPasses', 'DribblesAttempted', 'DribblesWon', 'AerialsWon', 'AerialsLost', 'TacklesSuccess', 'TacklesAttempted']

相关问题 更多 >