Python 无法 loop/search 现有 JSON 条目
Python unable to loop/search for existing JSON entry
还在学习python。我正在构建一个 IPFS 哈希描述主列表。每次我创建一个新的 IPFS 哈希时,我都可以将带有描述的哈希添加到这个列表中,我可以在以后搜索以找到特定的哈希。我还试图确保在添加新哈希之前 JSON 文件中不存在相同的现有哈希。任何帮助将不胜感激,我可以学习如何在未来扩展这一点。
这是我拥有并将搜索、添加和删除的 JSON 文件。
{
"hashlist": [
{
"description": "Test Video",
"ipfsHash": "QmVZATT8jWoCsNKzy2V3kwBrGXBjuKfifvrE",
"url": ""
},
{
"description": "Cat Photo",
"ipfsHash": "QmVqpEomPZBgQ8dU8cpNezxZHG2oc3xQi61P2n",
"url": ""
},
{
"description": "Test Dir",
"ipfsHash": "QmYdWbq65R4CdFqWGYnPA7V12bX7hf2zxv64AG",
"url": ""
}
]
}%
我可以使用创建的新散列添加到此列表,但是我无法搜索现有散列列表来查找散列或确保新散列未输入两次。
#!/usr/bin/python
import json
import os
data = []
if os.stat("hash.json").st_size != 0 :
file = open('hash.json', 'r')
data = json.load(file)
# print(data)
choice = raw_input("What do you want to do? \n a)Add a new IPFS hash \n s)Seach stored hashes \n\n >>")
if choice == 'a':
# Add a new hash.
description = raw_input('Enter hash description: ')
ipfsHash = raw_input('Enter IPFS hash: ')
entry = {'description': description , 'ipfsHash': ipfsHash}
# search existing hash listings here
xglobal = 0
for x in data["hashlist"]:
if data["hashlist"][xglobal]["ipfsHash"] == ipfsHash:
print "Hash already exist!"
break
else:
xglobal += 1
data['hashlist'].append(entry)
file = open('hash.json', 'w')
json.dump(data, file, sort_keys = True, indent = 4, ensure_ascii = False)
file.close()
print('IPFS Hash Added.')
pass
elif choice == 's':
# Search the current desciptions.
searchTerm = raw_input('Enter search term: ')
file = open('hash.json', 'r')
data = json.load(file)
file.close()
# Search working
sglobal = 0
for x in data["hashlist"]:
if data["hashlist"][sglobal]["description"] == searchTerm:
hash = data["hashlist"][sglobal]["ipfsHash"]
print "Hash Requested:", hash
break
else:
sglobal += 1
# Notes: How JSON is readable
# Show Hashes working !
# print data["hashlist"][0]["ipfsHash"]
# print data["hashlist"][1]["ipfsHash"]
# print data["hashlist"][2]["ipfsHash"]
# etc...
# Show Descriptions working !
# print data["hashlist"][0]["description"]
# print data["hashlist"][1]["description"]
# print data["hashlist"][2]["description"]
# etc...
因为我看不到列表条目顺序的重要性,所以我宁愿摆脱列表并改用字典。这样我就不必遍历列表并搜索 'hash-val'.
的存在
假设 hash_values 是唯一的,我的结构如下:
{
"hashlist": {
"QmVZATT8jWoCsNKzy2V3kwBrGXBjuKfifvrE" : {
"description": "Test Video",
"url": ""
},
"QmVqpEomPZBgQ8dU8cpNezxZHG2oc3xQi61P2n" : {
"description": "Cat Photo",
"url": ""
},
"QmYdWbq65R4CdFqWGYnPA7V12bX7hf2zxv64AG" : {
"description": "Test Dir",
"url": ""
}
}
}
这样,在插入新条目时,如果 hash-value 或 'key' 是否存在,我可以轻松地查询字典。
类似于:
if new_hash_val not in data["hashlist"]:
#proceed with adding entry
data["hashlist"][new_hash_val] = {"description":"..." , "url":"..."}
还在学习python。我正在构建一个 IPFS 哈希描述主列表。每次我创建一个新的 IPFS 哈希时,我都可以将带有描述的哈希添加到这个列表中,我可以在以后搜索以找到特定的哈希。我还试图确保在添加新哈希之前 JSON 文件中不存在相同的现有哈希。任何帮助将不胜感激,我可以学习如何在未来扩展这一点。
这是我拥有并将搜索、添加和删除的 JSON 文件。
{
"hashlist": [
{
"description": "Test Video",
"ipfsHash": "QmVZATT8jWoCsNKzy2V3kwBrGXBjuKfifvrE",
"url": ""
},
{
"description": "Cat Photo",
"ipfsHash": "QmVqpEomPZBgQ8dU8cpNezxZHG2oc3xQi61P2n",
"url": ""
},
{
"description": "Test Dir",
"ipfsHash": "QmYdWbq65R4CdFqWGYnPA7V12bX7hf2zxv64AG",
"url": ""
}
]
}%
我可以使用创建的新散列添加到此列表,但是我无法搜索现有散列列表来查找散列或确保新散列未输入两次。
#!/usr/bin/python
import json
import os
data = []
if os.stat("hash.json").st_size != 0 :
file = open('hash.json', 'r')
data = json.load(file)
# print(data)
choice = raw_input("What do you want to do? \n a)Add a new IPFS hash \n s)Seach stored hashes \n\n >>")
if choice == 'a':
# Add a new hash.
description = raw_input('Enter hash description: ')
ipfsHash = raw_input('Enter IPFS hash: ')
entry = {'description': description , 'ipfsHash': ipfsHash}
# search existing hash listings here
xglobal = 0
for x in data["hashlist"]:
if data["hashlist"][xglobal]["ipfsHash"] == ipfsHash:
print "Hash already exist!"
break
else:
xglobal += 1
data['hashlist'].append(entry)
file = open('hash.json', 'w')
json.dump(data, file, sort_keys = True, indent = 4, ensure_ascii = False)
file.close()
print('IPFS Hash Added.')
pass
elif choice == 's':
# Search the current desciptions.
searchTerm = raw_input('Enter search term: ')
file = open('hash.json', 'r')
data = json.load(file)
file.close()
# Search working
sglobal = 0
for x in data["hashlist"]:
if data["hashlist"][sglobal]["description"] == searchTerm:
hash = data["hashlist"][sglobal]["ipfsHash"]
print "Hash Requested:", hash
break
else:
sglobal += 1
# Notes: How JSON is readable
# Show Hashes working !
# print data["hashlist"][0]["ipfsHash"]
# print data["hashlist"][1]["ipfsHash"]
# print data["hashlist"][2]["ipfsHash"]
# etc...
# Show Descriptions working !
# print data["hashlist"][0]["description"]
# print data["hashlist"][1]["description"]
# print data["hashlist"][2]["description"]
# etc...
因为我看不到列表条目顺序的重要性,所以我宁愿摆脱列表并改用字典。这样我就不必遍历列表并搜索 'hash-val'.
的存在
假设 hash_values 是唯一的,我的结构如下:
{
"hashlist": {
"QmVZATT8jWoCsNKzy2V3kwBrGXBjuKfifvrE" : {
"description": "Test Video",
"url": ""
},
"QmVqpEomPZBgQ8dU8cpNezxZHG2oc3xQi61P2n" : {
"description": "Cat Photo",
"url": ""
},
"QmYdWbq65R4CdFqWGYnPA7V12bX7hf2zxv64AG" : {
"description": "Test Dir",
"url": ""
}
}
}
这样,在插入新条目时,如果 hash-value 或 'key' 是否存在,我可以轻松地查询字典。
类似于:
if new_hash_val not in data["hashlist"]:
#proceed with adding entry
data["hashlist"][new_hash_val] = {"description":"..." , "url":"..."}