[英]Printing json results in python
我正在為該項目使用兩個文件。 Snapchat.py和test.py
Snapchat.py包含以下內容(顯示了重要部分):
def add_friend(self, username):
"""Add user as friend
Returns JSON response.
Expected messages:
Success: '{username} is now your friend!'
Pending: '{username} is private. Friend request sent.'
Failure: 'Sorry! Couldn't find {username}'
:param username: Username to add as a friend
"""
r = self._request('friend', {
'action': 'add',
'friend': username,
'username': self.username
})
return r.json()
我當前正在處理的文件test.py具有以下代碼:
#including Snapchat in test.py
from snapchat import Snapchat
s = Snapchat()
username = raw_input('Enter your username: ')
password = raw_input('Enter your password: ')
friend = raw_input('Enter friend name: ')
s.login(username, password)
s.add_friend(friend)
這里的重要部分是:
Returns JSON response.
Expected messages:
Success: '{username} is now your friend!'
Pending: '{username} is private. Friend request sent.'
Failure: 'Sorry! Couldn't find {username}'
我希望該響應打印在命令外殼的test.py文件的末尾。
盡管我不知道如何執行此操作,但是我嘗試將其導入到test.py文件中並進行打印,但無法正常工作。
請注意,您粘貼在問題中的消息實際上將由add_friend方法返回,因此您無需執行任何操作。
只需像打印它們一樣;
#First you need to see what is the response for your login call
login_response = s.login(username, password)
print str(login_response)
#if you wish to access some value in this response
value = login_response.get("the_value", {the default value})
if value: #incase you want to make sure you logged in correctly
json_response = s.add_friend(friend)
print str(json_response)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.