[英]Why does this python procedure not produce an output after I have parsed the JSON file?
Using this json data I parsed the information correctly but after assigning the parsed data to a variable and running it through the procedure I dont get an output, why? 使用此json数据,我正确地解析了信息,但是在将解析的数据分配给变量并通过过程运行它之后,我没有得到输出,为什么?
{"maps":[{"id":"blabla i am spartacus","iscategorical":"0"},{"id":"blabla","iscategorical":"0"}],
"masks":{"id":"valore"},
"om_points":"value",
"parameters":{"id":"valore"}
}
Here is my code: 这是我的代码:
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
By the way, the name 'json_list' is a horrible name for a string. 顺便说一句,名称“ json_list”是字符串的可怕名称。 And in python, string_search() is called a function .
在python中,string_search()称为function 。 And a function should take some inputs, and return a result--it should not read global variables like json_list.
函数应该接受一些输入,并返回结果-它不应读取json_list之类的全局变量。
The pro way to write your function would be: 编写函数的专业方法是:
def string_search(a_string):
return a_string.count("i") >= 1
And then you would call the function like this: 然后您将这样调用函数:
result = string_search("hi")
Or even: 甚至:
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.