[英]python Questions about passing Python dictionary parameters
{
"originProduct": {
"statusType": "SALE",
"saleType": "NEW",
"leafCategoryId": "50002322",
"name": "Jabra SPEAK 750 블루투스스피커/스피커폰/음성회의스피커폰/JABRA / 자브라 공식정품",
"images": {
"representativeImage": {
"url": "http://shop1.phinf.naver.net/20221220_24/1671526069078ktTkT_JPEG/4172814067322311_1866531646.jpg"
},
"optionalImages": [
{
"url": "http://shop1.phinf.naver.net/20221220_2/16715260691656YLKl_JPEG/4172814155176861_2054960625.jpg"
},
{
"url": "http://shop1.phinf.naver.net/20221220_5/1671526069249T4zWk_JPEG/4172814239069085_483270929.jpg"
}
]
}
}
我有一個像上面那樣的 json 文件。
def open(self):
with open(self.json_file,"r",encoding='utf-8-sig') as f:
item_dic=json.load(f)
return item_dic
def save(self,item_dic):
with open(self.json_file,'w',encoding='utf-8-sig') as f:
json.dump(item_dic,f,indent=4,ensure_ascii=False)
def basic_data(self):
item_dic=self.open()
statusType = "SALE" #상품상태
saleType = "NEW" #판매유형
leafCategoryId = self.soup()["category"]["categoryId"] #카테고리넘버
name = self.soup()["name"] #상품명
salePrice = self.soup()["salePrice"] #판매가
stockQuantity = 100 #재고수량
basic_data = {
"statusType": statusType,
"saleType": saleType,
"leafCategoryId": leafCategoryId,
"name": name,
"salePrice": salePrice,
"stockQuantity": stockQuantity,
}
try:
del item_dic["originProduct"]["productLogistics"]
del item_dic["originProduct"]["saleStartDate"]
del item_dic["originProduct"]["saleEndDate"]
except:
pass
item_dic["originProduct"].update(basic_data)
self.save(item_dic)
在 basic_data 函數中,我們創建一個 json 類型並實現一個加載和更新 json 文件的函數。
我想將 def open 函數和 def save 函數合並為一個,以便它們可以運行。
def save_func(self,key,new_data):
## key -> parameter like ["originProduct"] ##
with open(self.json_file,"r",encoding='utf-8-sig') as f:
item_dic=json.load(f)
item_dic.[key].update(basic_data)
with open(self.json_file,'w',encoding='utf-8-sig') as f:
json.dump(item_dic,f,indent=4,ensure_ascii=False)
問題是作為key傳遞的參數如果是["originProduct"]是沒有問題的,但是傳遞兩個或者三個的時候,函數內部是否獲取key值就有問題了。
據我所知,您似乎希望根據提供給您的內容重載函數並更新item_dict
中的值。 我會警告你,我不習慣在 python 中重載,所以可能有更好的方法來做到這一點。 然而,我制作了一個基於數據更新的腳本作為示例,但我不確定我是否做的比你需要的更多,但我覺得答案就在某處。
main_list = {
"key1": 43,
"key2": 54,
"key3": 95
}
def update(param1, param2 = None):
if isinstance(param1, dict): # If parameter 1 is a dictionary, we have our key/data in param1
update_from_dictionary(param1)
elif isinstance(param1, list) and isinstance(param2, list): # If parameter 1 and 2 are both arrays, we were fed keys in param1 and values in param2
update_values(param1, param2)
else: # Assuming that if the above two conditions are not met, we were fed a singular key and value pair
update_value(param1, param2)
def update_values(keys, values):
# Handle updating your dictionary object by looping key/value pair using a for loop
for i in range(0, len(keys)):
main_list[keys[i]] = values[i]
def update_value(key, value):
main_list[key] = value
def update_from_dictionary(dictionary):
for i in dictionary.keys():
main_list[i] = dictionary[i]
def main():
print(main_list)
data = {
"key1": 1,
"key2": 2
}
update(data)
print(main_list)
if __name__ == "__main__":
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.