我只是想将二进制文件解析为Python。 这是我的程序的代码:
try:
with open('saves/temp/data.bin', 'r') as file:
confirmed_data = json.load(file)
except:
pass
confirmed_mode = confirmed_data[0]
confirmed_location = confirmed_data[1]
confirmed_units_data = confirmed_data[2]
confirmed_units = ast.literal_eval(confirmed_units_data)
confirmed_unit_quantities_data = confirmed_data[3]
confirmed_unit_quantities = ast.literal_eval(confirmed_unit_quantities_data)
我尝试了以下data.bin
,但效果很好:
["PLANETARY", "Naboo", "{'Optimal': [], 'Default': ['B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids']}", "{'B1 Battle Droids': 100}"]
然而,当我尝试下面的数据时,它不起作用,我不知道为什么,因为它基本上与我上面使用的数据完全相同,但成功运作:
["PLANETARY", "Kamino", {"Optimal": ["Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids"], "Default": ["B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids"]}, {"Aqua Droids": 60, "B1 Battle Droids": 40}]
我看过类似的SO问题,没有找到解决问题的方法。我检查过data.bin
是否有任何不是 Python文字(即十进制数据类型或变量名称)但没有找到任何内容。我不确定为什么第一个有效,第二个没有。我正在使用Python 3.6。
答案 0 :(得分:0)
正如 Daniel Pryden 在评论中提到的,数据是不同的。第一个 bin 的某些部分被引用为字符串,而第二个 bin 是标准的 JSON。
我首先想出了一种对两个不同文件进行分类的方法。我发现完全 JSON 是手动生成的默认值,部分字符串是从配置文件生成的。
因此,我在配置文件中添加了一个 'config' 标志,
["PLANETARY", "Naboo", "{'Optimal': [], 'Default': ['B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids', 'B1 Battle Droids']}", "{'B1 Battle Droids': 100}", "config"]
以及默认文件的“手动”标志,
["PLANETARY", "Kamino", {"Optimal": ["Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids", "Aqua Droids"], "Default": ["B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids", "B1 Battle Droids"]}, {"Aqua Droids": 60, "B1 Battle Droids": 40}, "manual"]
然后我使用以下代码来识别使用标志的配置文件,并对文件的字符串部分执行 ast.literal_eval
。如果使用手册文件,则跳过此文件并按原样使用标准 JSON。
import ast
try:
with open('saves/temp/data.bin', 'r') as file:
confirmed_data = json.load(file)
except:
pass
confirmed_mode = confirmed_data[0]
confirmed_location = confirmed_data[1]
if confirmed_data[4] == 'config':
confirmed_units_data = confirmed_data[2]
confirmed_units = ast.literal_eval(confirmed_units_data)
confirmed_unit_quantities_data = confirmed_data[3]
confirmed_unit_quantities = ast.literal_eval(confirmed_unit_quantities_data)
else:
confirmed_units = confirmed_data[2]
confirmed_unit_quantities = confirmed_data[3]