Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
__Python中的init_u; autorun方法_Python_Oop - Fatal编程技术网

__Python中的init_u; autorun方法

__Python中的init_u; autorun方法,python,oop,Python,Oop,我试图解析rss,并用特定的单词对它们的标题进行df 我的代码有4个部分 第1部分,初始化函数 第二部分(它是类新闻的方法),获取标题方法 第三部分(它是类新闻的方法),编写所有新闻的csv文件,并用熊猫阅读 第四部分(它是类新闻的方法),通过特定的单词在熊猫数据框中搜索 我的目的是自动运行(自动启动)我的三种方法:打印标题,写和读和某些单词 c=News(我的rss目录,target1,target2,'filename.csv')方法\uuuuuu init\uuuuu但这不会作为输出返回任

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

我的代码有4个部分

第1部分,初始化函数

第二部分(它是
类新闻
的方法),获取标题方法

第三部分(它是
类新闻的方法
),编写所有新闻的csv文件,并用熊猫阅读

第四部分(它是
类新闻的方法
),通过特定的单词在熊猫数据框中搜索

我的目的是自动运行(自动启动)我的
三种方法
打印标题
写和读
某些单词

c=News(我的rss目录,target1,target2,'filename.csv')
方法
\uuuuuu init\uuuuu
但这不会作为
输出返回任何内容

当我使用

c = News(my_dict_of_rss,target1,target2,'filename.csv')
c.打印标题测试()
c.写和读()
c.某些单词()
可以单独使用

TL;DR
中有1个
方法和3个
其他方法
,为什么它们没有通过启动包含所有
参数的
对象自动运行,而是单独启动


我的错在哪里?

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

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()


write_和
某些单词()
函数都有返回值,但是当您从
\uuu init\uuu()
调用它们时,您不会将这些返回值保存在任何地方。您有两种方法
返回那里的值,您希望如何获得该输出?另外,默认情况下会忽略
\uuuu init\uuu
方法的返回值;类的实例化总是导致类的实例,而不是其他值的实例。@JohnGordon@deceze好的问题在
return
中,我能解决这个问题吗?如何获取不带
返回的数据帧
?在函数末尾,您可以说
self.function\u result=value
,而不是说
返回值
。然后其他方法可以访问
self.function\u result
以获取值。(当然,您必须为每个实例使用不同的名称。)如果它基本上完成了实例化的所有工作,并且之后您并不真正需要该实例,那么它是一个函数,而不是一个类。
 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
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]
c = News(my_dict_of_rss,target1,target2,'filename.csv')
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()