python - 读取文件行并将其放在数组中

时间:2015-01-05 08:24:18

标签: python

我收到了以下代码,

#!/usr/bin/python
from twitter import *

OAUTH_TOKEN = 'my oauth token'
OAUTH_SECRET = 'my oauth secret'
CONSUMER_KEY = 'my consumer key'
CONSUMER_SECRET = 'my consumer secret'

USERNAME =  "my username"
LIST_NAME = "fud"

def main():

  twitter = Twitter(api_version='1.1', auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY,     CONSUMER_SECRET))
  try:

    # view a users lists
    lists = twitter.lists.list(user=USERNAME)
    # create a list
    create_list = twitter.lists.create(name=LIST_NAME)
    # view list members
    list_members = twitter.lists.members(slug=LIST_NAME, owner_screen_name=USERNAME)
    # add single user to list
    twitter.lists.members.create(owner_screen_name=USERNAME, slug=LIST_NAME, screen_name="microsoft")

    # add multiple users to list 
    twitter.lists.members.create_all(owner_screen_name=USERNAME, slug=LIST_NAME, screen_name="twitter,python")


  except TwitterHTTPError as twerr:
    import zlib
    print twerr, zlib.decompress(twerr.response_data, 16+zlib.MAX_WBITS) # gunzip response error info 

main()

代码的这一部分

    twitter.lists.members.create_all(owner_screen_name=USERNAME, slug=LIST_NAME, screen_name="twitter,python")

我如何用文件中的值替换screen_name =“twitter,python”值,比如读取screennames.txt行并放置所有值screen_name =“values,values..etc”

请告知

1 个答案:

答案 0 :(得分:2)

with open('screennames.txt') as namefile:
    screennames = ','.join(map(str.rstrip, namefile.readlines()))