假设您的数据如下所示:
{"attributes": "xyz", "hostname": ":hostname1.expedite.someserver.com"}
{"attributes": "xyz", "hostname": ":hostname1.expedite.another.com"}
{"attributes": "xyz", "hostname": ":hostname1.expedite.server.com"}
{"attributes": "xyz", "hostname": ":hostname1.expedite.we.com"}
{"attributes": "xyz", "hostname": ":hostname1.expedite.dont.com"}
{"attributes": "xyz", "hostname": ":hostname1.expedite.care.com"}
我们可以:
import ast
check = ".someserver.com"
with open("string.txt", "r") as f:
line = f.readline()
while line:
if check in line:
print(dict(ast.literal_eval(line))["hostname"])
line = f.readline()
这会打印我们:
:hostname1.expedite.someserver.com
假设数据如下:
[{"attributes": "xyz", "hostname": ":hostname1.expedite.someserver.com"}, {"attributes": "xyz", "hostname": ":hostname1.expedite.another.com"}, {"attributes": "xyz", "hostname": ":hostname1.expedite.server.com"}, {"attributes": "xyz", "hostname": ":hostname1.expedite.we.com"}, {"attributes": "xyz", "hostname": ":hostname1.expedite.dont.com"}, {"attributes": "xyz", "hostname": ":hostname1.expedite.care.com"}]
然后我们可以:
import json
check = ".someserver.com"
data = json.load(open("string2.txt", "r"))
for d in data:
if check in d["hostname"]:
print(d["hostname"])
这给了我们:
:hostname1.expedite.someserver.com