如何在Python中将变量用作文件名的一部分?

时间:2018-01-11 09:40:00

标签: python

我正在创建一个Python程序,它会向我发送我最喜欢的Subreddits帖子的十大列表。目前这是代码:

def AskReddit():
    askReddit = requests.get('http://www.reddit.com/r/AskReddit/top.json',
    headers={'user-agent':'Mozilla/5.0'},
    auth=('TadaceAce','Password'),
    )

    n = 0
    for post in askReddit.json()['data']['children']:
        x[n] = post['data']['title']
        n += 1

        if (n == 10):
            break

        output = "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}".format(x[0],
                                                    x[1], x[2], x[3],
                                                    x[4], x[5], x[6],
                                                    x[7], x[8], x[9])

    return output

def todayILearned():
    todayilearned = requests.get('http://www.reddit.com/r/todayilearned/top.json',
    headers={'user-agent':'Mozilla/5.0'},
    auth=('TadaceAce','Password'),
    )

    n = 0
    for post in todayilearned.json()['data']['children']:
        x[n] = post['data']['title']
        n += 1

        if (n == 5):
            break

        output = "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}".format(x[0],
                                                    x[1], x[2], x[3],
                                                    x[4], x[5], x[6],
                                                    x[7], x[8], x[9])

    return output

我正在尝试干掉代码,因为我会添加越来越多的subreddits,但我对这段代码非常感兴趣:

def reddit(subreddit):
    todayilearned = requests.get('http://www.reddit.com/r/{}/top.json'.format(subreddit),
    headers={'user-agent':'Mozilla/5.0'},
    auth=('TadaceAce','Password'),
    )

    z = "%s.json" % subreddit

    n = 0
    for post in z()['data']['children']:
        x[n] = post['data']['title']
        n += 1

        if (n == 5):
            break

    output = "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}".format(x[0],
                                                    x[1], x[2], x[3],
                                                    x[4], x[5], x[6],
                                                    x[7], x[8], x[9])

    return output

你将如何解决这个问题?特别是for循环,因为我80%确定这是问题所在。

2 个答案:

答案 0 :(得分:0)

这里的问题是 方法z()['data']['children']

中的reddit(subreddit)

z是一个字符串,您将其用作可调用对象。

根据我的理解,你需要做的是,

z = todayilearned.json()

如果您能够从网址获取数据,并且如果json具有datachildren属性,并且x也应该初始化,那么这应该有效

答案 1 :(得分:0)

正如我在上面的评论中提到的那样,你的代码看起来有点随意,我也注意到你说你得到了前十名,然后你在输出中使用了10个x元素,但是你打破了在6之后循环。

以下功能应该按照您的实际要求进行。

def reddit(subreddit):
    response = requests.get('http://www.reddit.com/r/{}/top.json'.format(subreddit),
    headers={'user-agent':'Mozilla/5.0'},
    auth=('TadaceAce','Password'),
    )

    count = 0
    top_ten = []
    for post in response.json()['data']['children']:
        top_ten.append(post['data']['title'])
        count += 1

        if (count == 9):
            break

    output = '\n'.join(top_ten)

    return output

调用print(reddit('AskReddit'))会返回

What are life’s toughest mini games?
What screams "I'm emotionally unstable"?
What is something an 18 year old son can do to embarrass his parents?
Chefs of Reddit, what are the biggest ripoffs that your restaurants sell?
What's the dumbest thing you're willing to argue about?
Which famous Reddit Story do you believe to actually be 100% bullshit?
What is a common internet thing that you hate?
What is an imminent danger that nobody seems to be talking about?
What do you regret doing at university?