亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

__init__ Python 中的自動運行方法

__init__ Python 中的自動運行方法

喵喵時光機 2022-08-25 13:43:25
我試圖解析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部分(這是類新聞的方法),獲取標題方法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部分(這是類新聞的方法),編寫所有新聞的csv文件并在熊貓中讀取 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部分(這是類新聞的方法),通過某些單詞在pandas數據幀中搜索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]我的目的是自動運行(自動啟動)我的:,并且只是通過three methodsprint_headlines_testwrite_and_readcertain wordsc = 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個方法和3個,為什么它們沒有通過啟動對象自動運行,而是單獨啟動?__init__other methodsclassclassarguments我的錯在哪里?
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

我相信我們可以讓你的班級工作。您的代碼現在確實正在運行,但是由于您似乎只對 的返回值感興趣,因此可能必須單獨調用: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()


查看完整回答
反對 回復 2022-08-25
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號