【问题标题】:error while calling function inside another function在另一个函数中调用函数时出错
【发布时间】:2018-12-21 18:44:44
【问题描述】:

我有 newspaper3k 的函数,它提取给定 url 的摘要。给定为:-

def article_summary(row):
    url = row
    article = Article(url)
    article.download()
    article.parse()
    article.nlp()
    text = article.summary
    return text

我有一个名为url的列的熊猫数据框

url
https://www.xyssss.com/dddd
https://www.sbkaksbk.com/shshshs
https://www.ascbackkkc.com/asbbs
............
............

还有另一个函数main_code() 运行得非常好,我在其中使用article_summary。我想将article_summarymain_code() 这两个函数添加到一个函数final_code 中。

这是我的代码:第一个函数为:-

def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text

这是第二个功能

def main_code():

article_data['article']=article_data['url'].apply(article_summary)
return article_data['articles']

当我完成后:

def final_code():
    article_summary()
    main_code()

但是final_code() 没有给出任何输出,它显示为TypeError: article_summary() missing 1 required positional argument: 'row'

【问题讨论】:

    标签: python-3.x pandas user-defined-functions python-newspaper


    【解决方案1】:

    这些是您使用的实际网址吗?如果是这样,他们似乎会导致ArticleException,我用一些维基百科页面测试了你的代码并且它有效。

    请注意,您是否只使用一个 df?如果没有,最好将其作为变量传递给函数。

    ------------------------------------在cmets之后编辑--------- -------------------------------------------------- ------------ 我认为 Python 函数上的 tutorial 将是有益的。也就是说,关于您的具体问题,以您描述的方式调用函数将使其运行两次,在这种情况下不需要。正如我之前所说,您应该将 df 作为参数传递给函数,这里是 tutorial on global vs local variables 以及如何使用它们。

    您遇到的错误是因为您应该将参数“行”传递给函数 article_summary(请参阅函数教程)。

    【讨论】:

    • 如何同时调用两个函数 'article_summary' 和 'main_code'?因为函数 'article_summary' 需要在 'main_code()' 之前调用。
    • 我不明白这个问题。
    • 也编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多