如果用戶選擇這樣做,我正在撰寫將洗掉特定元素的代碼。它會洗掉括號中的資訊,但不會洗掉括號本身。有誰知道我做錯了什么?
`with open(filename, 'r') as fp:
data = json.load(fp)
for dict in data['songs']:
if song in dict["SongName"]:
songpath = (dict['Path'])
for stuff in data["songs"]:
if song in stuff["SongName"]:
del stuff["SongName"]
if stuff["Path"]==songpath:
del stuff["Path"]
with open(filename, 'w') as fp:
json.dump(data, fp,sort_keys=True, indent=4, separators=(",", ":"))`
它回傳什么:
"songs": [
{},
]
uj5u.com熱心網友回復:
添加一個檢查是否stuff為空。如果是,則從串列中洗掉該元素:
!cat "/content/sample_data/test.json"
[Out]:
{
"songs": [
{
"SongName": "a",
"Path": "/home"
}
]
}
song = "a"
with open("/content/sample_data/test.json", 'r') as fp:
data = json.load(fp)
for dict in data['songs']:
if song in dict["SongName"]:
songpath = (dict['Path'])
for stuff in data["songs"]:
if song in stuff["SongName"]:
del stuff["SongName"]
if stuff["Path"]==songpath:
del stuff["Path"]
if len(stuff.keys()) == 0:
data["songs"].remove(stuff)
with open("/content/sample_data/test_2.json", 'w') as fp:
json.dump(data, fp,sort_keys=True, indent=4, separators=(",", ":"))
!cat "/content/sample_data/test_2.json"
[Out]:
{
"songs":[]
}
uj5u.com熱心網友回復:
songs您可以使用串列理解輕松過濾掉 的內容:
data["songs"] = [song for song in data["songs"] if song]
uj5u.com熱心網友回復:
它會洗掉括號中的資訊,但不會洗掉括號本身。
我不知道如何對您使用的語言進行編程。但是,我確信您可以列印出整個結果并將其寫入檔案。然后,您可以使用其他語言將 exe 構建到 RpSubText。然后,您可以通過小 exe 將資料重寫到檔案中并寫入一個 ini 檔案,以便主程式可以接收到此操作已完成并可以讀取檔案。
while(GetKeyText(your path and ini)!="false")// false is a text because you read an ini file. If the exe is finished, it will trun it into true
SetKeyText(your path and ini,"false")//in order to reuse the ini
break()
我不會編程 C,這可能有語法錯誤,這只是一個例子,可能這種語言也可以 RpSubText,但我對此一無所知。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519422.html
標籤:Pythonjson
上一篇:添加列Heroku資料庫
