簡體   English   中英

解析JSON文件后,為什么此python過程不產生輸出?

[英]Why does this python procedure not produce an output after I have parsed the JSON file?

使用此json數據,我正確地解析了信息,但是在將解析的數據分配給變量並通過過程運行它之后,我沒有得到輸出,為什么?

{"maps":[{"id":"blabla i am spartacus","iscategorical":"0"},{"id":"blabla","iscategorical":"0"}],
"masks":{"id":"valore"},
"om_points":"value",
"parameters":{"id":"valore"}
}

這是我的代碼:

import json

json_data = open("json_file")
data = json.load(json_data)
json_data.close()

json_list = data ["maps"] [0] ["id"]

def string_search():
    if json_list.count("i") >= 1:
        return True
    return False
import json

json_data = open("data.txt")
data = json.load(json_data)
json_data.close()


json_list = data ["maps"] [0] ["id"]

print json_list   #blabla i am spartacus

def string_search():
    if json_list.count("i") >= 1:
        return True
    return False


result = string_search()
print result     #True

順便說一句,名稱“ json_list”是字符串的可怕名稱。 在python中,string_search()稱為function 函數應該接受一些輸入,並返回結果-它不應讀取json_list之類的全局變量。

編寫函數的專業方法是:

def string_search(a_string): 
    return a_string.count("i") >= 1

然后您將這樣調用函數:

result = string_search("hi")

甚至:

def string_search(a_string, char):
    return a_string.count(char) >= 1


result = string_search("hello", "l")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM