以文本文件python打印的筛选器返回结果

2024-09-27 00:19:56 发布

您现在位置:Python中文网/ 问答频道 /正文

使用此代码:

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\u 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\u friends()函数

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

Tags: fromimportinputyourgetrawusernamepassword
1条回答
网友
1楼 · 发布于 2024-09-27 00:19:56

您只需打印每个朋友的name条目。假设friends是一个列表:

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

或者你可以使用列表理解:

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

相关问题 更多 >

    热门问题