【发布时间】:2017-03-23 08:50:54
【问题描述】:
我目前正在编写一些 Python 代码,以获取 JSON 文件的某些文本元素并将此文本以 'spell name': 'description' 格式写入文件。例如,采用以下 JSON 文件的名称和简短描述元素(为简洁而编辑):
[
{
"pk": 0,
"model": "srd20.spell",
"fields": {
"description": "<p>An arrow of acid springs from your hand and speeds to its target. You must succeed on a <a href=\"../combat.html#_ranged-touch-spells-in-combat-95\">ranged touch attack</a> to hit your target. The arrow deals 2d4 points of acid damage with no splash damage. For every three caster levels you possess, the acid, unless neutralized, lasts for another round (to a maximum of 6 additional rounds at 18th level), dealing another 2d4 points of damage in each round.</p>",
"school": "conjuration",
"saving_throw": "none",
"name": "Acid Arrow",
"reference": "prd/spells",
"level": "sorcerer/wizard 2",
"spell_resistance": "no",
"area": "",
"casting_time": "1 standard action",
"effect": "one arrow of acid",
"descriptor": "acid",
"range": "long (400 ft. + 40 ft./level)",
"short_description": "Ranged touch attack; 2d4 damage for 1 round + 1 round/3 levels.",
"components": "V, S, M (rhubarb leaf and an adder's stomach), F (a dart)",
"altname": "acid-arrow",
"duration": "1 round + 1 round per three levels",
"subschool": "creation",
"target": ""
}
},
...
]
并将这些详细信息写入文件,如下所示:
'Acid Arrow': 'Ranged touch attack; 2d4 damage for 1 round + 1 round/3 levels.',
到目前为止,我所拥有的似乎几乎可以解决问题。它采用 JSON,遍历每个元素(列表中的拼写)并将该拼写的名称和描述以我想要的格式写入文件 spelllistparsed.txt:
import json
fWrite = open('spelllistparsed.txt', 'w')
with open('spells.json') as data_file:
data = json.load(data_file)
for count, item in enumerate(data, start=0):
fWrite.write("'" + data[count]["fields"]["name"] + "': " + "'"
+ data[count]["fields"]["short_description"] + "',\n")
但问题是当我运行程序时,它只会将列表的一部分写入新文件。当我运行它时,最终结果将显示应该存在的大约 30 个咒语。经过反复试验,我发现了以下几点:
- 无论我使用哪种方法进行迭代,这似乎都会发生。我尝试过使用整数计数,
for spell in data:但它们会导致相同的结果。还尝试增加 2 - 每隔一个咒语就会被写入,但在大约 50 个左右的咒语后仍然会被切断。 - 当我对法术使用不同的第二个字段(不是“short_description”,而是法术的不同元素,如“description”或“effect”)时,会向文件中写入不同数量的文本。对于短文本,所有 1000 个左右的
spell name : second field都将显示,而对于第二个字段中的大量文本,它只会将几个拼写写入spelllistparsed.txt。我认为写入文件的内容没有特定的字符限制,但这似乎是问题的一部分 - 当我将第二个字段设置为始终打印相同的内容 (
data[0]["fields"]["short_description"]) 时,它成功地将所有拼写作为'spell name': 'description for spell 0'写入文件,因此问题可能在于处理许多不同的描述 - 据我所知 JSON 是有效的,这应该是可能的(通过 JSONLint 运行)
- 在运行当前代码时,它会通过 Baleful Polymorph 写入法术 Acid Arrow
任何帮助将不胜感激!我觉得这应该相对简单,但这是我第一次使用 Python,到目前为止我寻找解决方案没有结果。让我知道我是否应该在此处包含任何其他可以提供帮助的信息。
咒语的 JSON 文件:https://github.com/machinalis/django-srd20/blob/master/pathfinder/spells.json
【问题讨论】:
标签: python json python-2.7