我正在嘗試使用 json 檔案來填充我的資料庫,但是當我使用
from json import loads
with open(path.join(path.abspath("..\Data"), "monsters.json" ) as f:
print(f.read())
json_file = loads(f.read())
我收到上述錯誤。我對這個錯誤的理解是它說我試圖通過的要么沒有使用正確的 json 語法,要么在輸入中沒有任何資料。
這是json檔案的代碼片段:
[
{
"name": "Aboleth",
"meta": "Large aberration, lawful evil",
"ac": "17 (Natural Armor)",
"hp": "135 (18d10 36)",
"Speed": "10 ft., swim 40 ft. ",
"STR": "21",
"strMod": "( 5)",
"DEX": "9",
"dexMod": "(-1)",
"CON": "15",
"conMod": "( 2)",
"INT": "18",
"intMod": "( 4)",
"WIS": "15",
"wisMod": "( 2)",
"CHA": "18",
"chaMod": "( 4)",
"savingThrow": "CON 6, INT 8, WIS 6",
"Skills": "History 12, Perception 10",
"Senses": "Darkvision 120 ft., Passive Perception 20",
"Languages": "Deep Speech, Telepathy 120 ft.",
"Challenge": "10 (5,900 XP)",
"Traits": "<p><em><strong>Amphibious.</strong></em> The aboleth can breathe air and water. </p><p><em><strong>Mucous Cloud.</strong></em> While underwater, the aboleth is surrounded by transformative mucus. A creature that touches the aboleth or that hits it with a melee attack while within 5 feet of it must make a DC 14 Constitution saving throw. On a failure, the creature is diseased for 1d4 hours. The diseased creature can breathe only underwater. </p><p><em><strong>Probing Telepathy.</strong></em> If a creature communicates telepathically with the aboleth, the aboleth learns the creature's greatest desires if the aboleth can see the creature.</p>",
"Actions": "<p><em><strong>Multiattack.</strong></em> The aboleth makes three tentacle attacks. </p><p><em><strong>Tentacle.</strong></em> <em>Melee Weapon Attack:</em> 9 to hit, reach 10 ft., one target. <em>Hit:</em> 12 (2d6 5) bludgeoning damage. If the target is a creature, it must succeed on a DC 14 Constitution saving throw or become diseased. The disease has no effect for 1 minute and can be removed by any magic that cures disease. After 1 minute, the diseased creature's skin becomes translucent and slimy, the creature can't regain hp unless it is underwater, and the disease can be removed only by heal or another disease-curing spell of 6th level or higher. When the creature is outside a body of water, it takes 6 (1d12) acid damage every 10 minutes unless moisture is applied to the skin before 10 minutes have passed. </p><p><em><strong>Tail.</strong></em> <em>Melee Weapon Attack:</em> 9 to hit, reach 10 ft. one target. <em>Hit:</em> 15 (3d6 5) bludgeoning damage. </p><p><em><strong>Enslave (3/Day).</strong></em> The aboleth targets one creature it can see within 30 feet of it. The target must succeed on a DC 14 Wisdom saving throw or be magically charmed by the aboleth until the aboleth dies or until it is on a different plane of existence from the target. The charmed target is under the aboleth's control and can't take reactions, and the aboleth and the target can communicate telepathically with each other over any distance. </p><p>Whenever the charmed target takes damage, the target can repeat the saving throw. On a success, the effect ends. No more than once every 24 hours, the target can also repeat the saving throw when it is at least 1 mile away from the aboleth.</p>",
"legendaryActions": "<p>The aboleth can take 3 legendaryActions, choosing from the options below. Only one legendary action option can be used at a time and only at the end of another creature's turn. The aboleth regains spent legendaryActions at the start of its turn. </p><p><em><strong>Detect.</strong></em> The aboleth makes a Wisdom (Perception) check. </p><p><em><strong>Tail Swipe.</strong></em> The aboleth makes one tail attack. </p><p><em><strong>Psychic Drain</strong></em> (Costs 2 Actions). One creature charmed by the aboleth takes 10 (3d6) psychic damage, and the aboleth regains hp equal to the damage the creature takes.</p>",
"image": "https://media-waterdeep.cursecdn.com/avatars/thumbnails/0/11/1000/1000/636238825975375671.jpeg"
},
...
]
現在我嘗試過的一件事是使用以下方法清理資料:
with open(path.join(path.abspath("..\Data"), "monsters.json" ), encoding='utf-8') as f:
print(f.read().strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))
json_file = loads(f.read().strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))
但沒有運氣。
然后我嘗試只使用load(f),但我仍然收到相同的錯誤。
有什么我想念的嗎?無論如何感謝您的幫助!
編輯:根據評論請求,我編輯了 json 代碼片段,不僅包括前十行,還包括第一個條目。
我還使用了讀取片段:
with open(path.join(path.abspath("..\Data"), "monsters.json" ), encoding='utf-8') as f:
json_file = loads(f.read()..strip("'<>() ").replace('\'', '\"' ).encode('utf-8'))
它給了我一個新的錯誤:
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 25 column 604 (char 1244)
所以當看到這個時......我認為決議器沒有正確讀取特征條目,但我不知道為什么。
uj5u.com熱心網友回復:
最有可能的是,由于錯誤指向檔案中的第一個字符,因此資料以 BOM 開頭,否則如果,...洗掉末尾的 ,則資料是正確的。如果存在于 UTF-8 資料中,請使用以下命令洗掉 BOM:
import json
with open('monsters.json', encoding='utf-8-sig') as f:
data = json.load(f)
還值得注意的是,至少在 Python 3.9 上,如果utf-8-sig不使用,錯誤會得到改善:
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326681.html
