我的json檔案看起來是這樣的-
{"classes"/span>: ["BUSINESS","PLACE","HOLD","ROAD","Sub"。 "SUPER"/span>,"EA"/span>,"DIS"/span>。 "SUB","UNI"],"注釋"。 [["Khilg Khil Block A Dha M K Enter House 308/A Road 8 Til Rhy", {"物體"/span>: [[0, 8, "EA"/span>], [9, 17, "SUB"], [18, 25, "SUPER"], [26, 31, "DIS"], [32, 46, "BUSINESS"], [47, 58, "HOLDING"], [59, 65, "ROAD"], [66, 75, "Sub"]。[76, 82, "PLACE"]]}] 。
在這種情況下,我想讓這里的每一個字母都變成小寫,以獲得一個完整的檔案。但這并不奏效。 我的代碼-
data = json.load(open("data.json"/span>)
new_data = {}
for i in new_data.keys() 。
if type(new_data[i]) is list:
new_data[i]= [j.lower() for j in new_data[i]] 。
else:
new_data[i] = new_data[i].lower()
with open("data.json"/span>, 'w'/span>) as fp:
json.dump(new_data, fp)
uj5u.com熱心網友回復:
由于你的資料中存在串列,我認為使用遞回更好。
代碼:
def recursion_lower(x)。
if type(x) is str:
return x.lower()
elif type(x) is list:
return [recursion_lower(i) for i in x]
elif type(x) is dict:
return {recursion_lower(k):recursion_lower(v) for k,v in x.item()}.
else:
return x
data = {'classes': ['BUSINESS', 'PLACE', 'HOLD', 'ROAD', 'Sub', 'SUPER', 'EA', 'DIS', 'SUB', 'UNI'], '注釋': [['Khilg Khil Block A Dha M K Enter House 308/A Road 8 Til Rhy', {'物體': [[0, 8, 'EA'], [9, 17, 'SUB'], [18, 25, 'SUPER'], [26, 31, 'DIS'], [32, 46, 'BUSINESS'], [47, 58, 'HOLDING'], [59, 65, 'ROAD'], [66, 75, 'Sub'] 。[76, 82, 'PLACE']]}]}。
new_data = recursion_lower(data)
print(new_data)
輸出:
{'classes': ['business', 'place', 'hold', 'road', 'sub', 'super', 'ea', 'disc', 'sub', 'uni'], '注釋'。[['khilg khil block a dha m k enter house 308/a road 8 til rhy', {'物體': [[0, 8, 'ea'/span>], [9, 17, 'sub'], [18, 25, 'super'], [26, 31, 'dis'], [32, 46, 'business'], [47, 58, 'holding'], [59, 65, 'road'], [66, 75, 'sub'] 。[76, 82, 'place']]}]}。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309198.html
標籤:
下一篇:如何用畫布外導航條推送內容
