对文本文件中的字典列表进行排序



我目前有一个数组列表,其中包含一个文本文件中的字典列表。我试图导入json来使用内置函数,但它不与文本文件的格式工作。有什么建议吗?我的另一个策略是手动过滤价值,但我遇到了一个障碍,因为"资金";在公开场合和私下场合都被提及。

bankroll=[]
with open("abc.txt", "r") as b:
lines=b.readlines()
for line in lines:
if "bankroll" in line:
key, value = [x.strip() for x in line.strip().split(':', 1)]
bankroll.append[value]
[
{
"private": {
"bankroll": 1000,
"last_bid": 0,
"launching": false,
"name": "abc",
"tech": 0
},
"public": {
"auction_round": 0,
"game_stage": "start",
"last_mining_payoff": 0,
"last_winning_bid": 0,
"last_winning_bidders": [],
"last_winning_miner": "",
"max_rounds": 201,
"players": {
"AggressiveLauncher": {
"bankroll": 1000
},
"abc": {
"bankroll": 1000
},
"PassiveLauncher": {
"bankroll": 1000
},
"def": {
"bankroll": 1000
},
"SpongeBob": {
"bankroll": 1000
}
},
"round": 0
}
},

上面的例子是在文本文件中多个数组中的一个中,使用了多个的单数元素。

使用json应该很简单:

>>> lines = '''
[
{
"private": {
"bankroll": 1000,
"last_bid": 0,
"launching": false,
"name": "abc",
"tech": 0
},
"public": {
"auction_round": 0,
"game_stage": "start",
"last_mining_payoff": 0,
"last_winning_bid": 0,
"last_winning_bidders": [],
"last_winning_miner": "",
"max_rounds": 201,
"players": {
"AggressiveLauncher": {
"bankroll": 1000
},
"abc": {
"bankroll": 1000
},
"PassiveLauncher": {
"bankroll": 1000
},
"def": {
"bankroll": 1000
},
"SpongeBob": {
"bankroll": 1000
}
},
"round": 0
}
},
{
"private": {
"bankroll": 1000,
"last_bid": 0,
"launching": false,
"name": "abc",
"tech": 0
},
"public": {
"auction_round": 0,
"game_stage": "start",
"last_mining_payoff": 0,
"last_winning_bid": 0,
"last_winning_bidders": [],
"last_winning_miner": "",
"max_rounds": 201,
"players": {
"AggressiveLauncher": {
"bankroll": 1000
},
"abc": {
"bankroll": 1000
},
"PassiveLauncher": {
"bankroll": 1000
},
"def": {
"bankroll": 1000
},
"SpongeBob": {
"bankroll": 1000
}
},
"round": 0
}
}]
'''
>>>import json
>>>
>>> list_lines = json.loads(lines)
>>> 
>>> list_lines[0]
{'private': {'tech': 0, 'last_bid': 0, 'bankroll': 1000, 'name': 'abc', 'launching': False}, 'public': {'auction_round': 0, 'round': 0, 'max_rounds': 201, 'game_stage': 'start', 'last_mining_payoff': 0, 'last_winning_bid': 0, 'last_winning_miner': '', 'last_winning_bidders': [], 'players': {'PassiveLauncher': {'bankroll': 1000}, 'def': {'bankroll': 1000}, 'AggressiveLauncher': {'bankroll': 1000}, 'abc': {'bankroll': 1000}, 'SpongeBob': {'bankroll': 1000}}}}
>>> list_lines[1]
{'private': {'tech': 0, 'last_bid': 0, 'bankroll': 1000, 'name': 'abc', 'launching': False}, 'public': {'auction_round': 0, 'round': 0, 'max_rounds': 201, 'game_stage': 'start', 'last_mining_payoff': 0, 'last_winning_bid': 0, 'last_winning_miner': '', 'last_winning_bidders': [], 'players': {'PassiveLauncher': {'bankroll': 1000}, 'def': {'bankroll': 1000}, 'AggressiveLauncher': {'bankroll': 1000}, 'abc': {'bankroll': 1000}, 'SpongeBob': {'bankroll': 1000}}}}
>>> 
>>> list_lines[1]['private']['bankroll']
1000

在您的情况下,您可以使用json.load:

直接从文件对象反序列化。
>>> with open("abc.txt", "r") as b:
lines = json.load(b)

相关内容

  • 没有找到相关文章

最新更新