__Python中的init_u; autorun方法

2024-06-16 11:52:56 发布

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

我试图解析rss,并用特定的单词对它们的标题进行df

我的代码有4个部分

第1部分,初始函数

class News:
        def __init__(self, rss_dict, t1, t2, filename):
            self.rss_dict = rss_dict
            self.t1 = t1
            self.t2 = t2
            self.filename = filename
            self.print_headlines_test()
            self.write_and_read()
            self.certain_words()

第2部分(它是class News的方法),获取标题方法e

def print_headlines_test(self):
        for key,url in self.rss_dict.items():
            feed = feedparser.parse(url)

        headlines = []

        allheadlines = []

        for newsitem in feed['items']:
            headlines.append(newsitem['title'])

        for key,url in self.rss_dict.items():
            allheadlines.extend(headlines)
        self.allheadlines = allheadlines

第3部分(这是class News的方法),编写所有新闻的csv文件,并在pandas

 def write_and_read(self):
        header = ['Tittle'] 

        with open(self.filename, 'w', encoding='utf-8-sig') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')

            writer.writerow(i for i in header) 

            for a  in zip(self.allheadlines):
                writer.writerow((a))


            df = pd.read_csv(self.filename)

        self.df = df

        return df

第4部分(它是class News的方法),通过某些单词在熊猫数据框中搜索

def certain_words(self):
        result = self.df.apply(lambda x: x.str.contains(self.t1, na=False,
                                    flags = re.IGNORECASE, regex=True)).any(axis=1)
        result2 = self.df.apply(lambda x: x.str.contains(self.t2, na=False,
                                    flags = re.IGNORECASE, regex=True)).any(axis=1)
        return self.df[result&result2]

我的目的是自动运行(autolaunch)我的three methodsprint_headlines_testwrite_and_readcertain words,只需

c = News(my_dict_of_rss, target1,target2,'filename.csv')方法__init__,但这不会返回任何内容作为output

当我使用

c = News(my_dict_of_rss,target1,target2,'filename.csv')

c.print_headlines_test()c.write_and_read()c.certain_words()分别是工作

TL;DR有1__init__方法和3other methods{}方法,为什么它们没有通过启动带有所有argumentsclass对象自动运行,而是单独启动

我的错在哪里


Tags: csv方法inselfdfforreaddef
1条回答
网友
1楼 · 发布于 2024-06-16 11:52:56

我相信我们能让你们班开始工作。您的代码现在确实在运行,但由于您似乎只对来自certain_words()的返回值感兴趣,因此可能需要单独调用它:

class News:
    def __init__(self, rss_dict, t1, t2, filename):
        # init elided, but just these two functions called
        self.print_headlines_test()
        self.write_and_read()

    def print_headlines_test(self):
        # processing elided, except:
        self.allheadlines = allheadlines

    def write_and_read(self):
        # processing elided, except the next line (Note no return)
        self.df = df

    def certain_words(self):
        # processing elided, except for this return
        return self.df[result & result2]

# client code is now two lines:
c = News(my_dict_of_rss, target1, target2, 'filename.csv')
words = c.certain_words()

# If you don't care about keeping the instance 'c' around, then you can do it in one line:
words = News(my_dict_of_rss, target1, target2, 'filename.csv').certain_words()

相关问题 更多 >