我有一個python腳本,它向一個API發出一個http請求來拉取「成員」數據。然後它運行一個循環來打幾個不同的成員ID(在一個文本文件中列出),看他們每個人是否「過期」。我想要做的是打印出這些結果的摘要,而不是打印出每個成員的單獨結果。這裏是我的Python腳本:在Python中打印結果彙總
import requests
import json
qa_members = open("numbers_members.txt")
arraylist = []
for line in qa_members.readlines():
arraylist.extend(line.split())
qa_members.close()
arraylist = map(int, arraylist)
authorization_code = raw_input("please enter your authorization code: ")
members = arraylist
print "Member Check Starting"
for member in members:
url = "http://api22.wiki.com/v1.12/member?id="+str(member)
header = {"Authorization": authorization_code}
response = requests.get(url, headers=header)
member_check = json.loads(response.text)
final_member_status = member_check["response"]["member"]
["is_expired"]
print str(member) + " expired status: " + str(final_member_status)
if final_member_status == False:
print "All Members Passed"
else:
print "Some members are expired, please refer to log"
print "Member Check Complete"
由此看來,我得到幾個端子輸出,如:
123 expired status: False
567 expired status: False
有沒有辦法替代,打印出結果的總結?這可能是這樣的:
"24 members returned False"
"11 members returned True"
這也將有助於找出一個特定成員的ID,如果返回的狀態是「真」:
"Member 1234 returned True"
或者類似的東西。任何幫助是極大的讚賞。
謝謝!
謝謝!這工作:) – user7681184