0

このコードの使用:

from pysnap import Snapchat
from pprint import pprint

s = Snapchat()

username = raw_input('Enter your username: ')
password = raw_input('Enter your password: ')
s.login(username, password)
friends = s.get_friends()

newfile = open("newfile.txt", "a")
pprint(friends, newfile)

get_friends() の戻り値/結果を出力します。これは素晴らしいことです。ほぼ正確に私が望むものです。

しかし。

結果は次のようになります。

{u'can_see_custom_stories': True,
  u'direction': u'OUTGOING',
  u'display': u'',
  u'name': u'a7twini', #a7twini being the username here
  u'type': 0},

表示したい唯一のものは次のとおりです。

a7twini
<nextusername>
<nextusername>
etc..

これらの結果を「フィルタリング」する方法はありますか? 名前だけがテキスト ファイルに残ることを確認しますか?

誰かが私を助けてくれることを願っています、ありがとう!

編集:誰かが get_friends() 関数を求めました

def get_friends(self):
    """Get friends
    Returns a list of friends.
    """
    return self.get_updates().get('friends', [])
4

1 に答える 1

1

name友達ごとにエントリを印刷するだけです。リストであると仮定friendsします:

for friend in friends:
    newfile.write(friend[u'name'] + "\n")

または、リスト内包表記を使用できます。

newfile.writelines([friend[u'name'] + "\n" for friend in friends])
于 2014-11-19T12:16:45.407 に答える