建立一个类:building_dict() 接受 1 个位置参数,但给出了 2 个

Posted

技术标签:

【中文标题】建立一个类:building_dict() 接受 1 个位置参数,但给出了 2 个【英文标题】:Building a class: building_dict() takes 1 positional argument but 2 were given 【发布时间】:2020-07-09 20:15:57 【问题描述】:

我正在构建一个类,用于清除从 Instagram cmets 抓取的原始数据并将它们构建到字典中。该类应按顺序执行最后一个def building_dict() 中使用的许多函数。但是,我遇到了这个问题:building_dict() takes 1 positional argument but 2 were given

我已经尝试过在__init__ 中移动.selfrawdata,但似乎我的错误深深隐藏在代码中。

我使用的原始数据只是在执行 preprocessing() 之后:test_1 = ['vit4017\nLike always\n16h2 likesReply']。

当前代码的结果应该是 id 'Username': _, 'Comment': _, 'Date_back': _, 'Likes': _

我觉得卡住了,提前谢谢你。

    import re

class InstagramCleaning(object):
    def __init__(self, rawdata):
        self.rawdata = rawdata
        self.result = self.building_dict(rawdata)
        return result 

#Splitting the comment and removing the word "ответить"
    def preprocessing(rawdata):
        notSorted = [comment.text.split('Ответить') for comment in rawdata]
        allin = [word for comment in notSorted for word in comment]
        return allin

#Removing the user from the list (so we can work with the data in the comment) 
    def excluding_user(allin):
        rest = [rest for comment in allin for _, rest in [comment.split('\n', 1)]]
        return rest

#Getting dates out of the whole comment string 
    def filter_dates(rest):
        dates = []
        for date_back in rest:
            #for those posts that do not have any likes
            if bool(re.search(r"[0-9]+[a-zA-Z]+Reply", date_back.rsplit('\n', 1)[1])) == True: 
                my_findings = re.search(r"[0-9]+[a-zA-Z]+Reply", date_back.rsplit('\n', 1)[1])
                dates.append(my_findings.group().split('Reply',1)[0])
                # for those posts that do have likes 
                if date_back.split()[-1] in ('likesReply', 'likeReply'): 
                    dates.append(date_back.split()[-2][:-1])
        return dates
    #оставшиеся
#Getting likes out of the whole comment string 
    def filter_likes(rest):
        likes = [like.split()[-2][-1] if like.split()[-1] in ("likesReply",'likeReply') else 'None' for like in rest]
        return likes

    #Getting the username from the whole string

    def username(allin):
        username = [ user for comment in allin for user, _ in [comment.split('\n', 1)]]
        return username

    #оставшиеся
    #Getting the text of the comment out
    def comment_text(rest):
        comment_text = [comment.rsplit('\n', 1)[0] for comment in rest]
        return comment_text

    #Finally building dictionary
    def building_dict(self):
        allin = preprocessing(rawdata)
        rest = excluding_user(allin)
        dates = filter_dates(rest)
        likes = filter_likes(rest)
        username = username(allin)
        comment = comment_text(rest)
        all_data = list(zip(username, comment, dates, likes))
        dicts = i: 'Username' : usernames_and_comments[i][0], 'Comment':  usernames_and_comments[i][1], 'Dates back': usernames_and_comments[i][2], 'Likes': usernames_and_comments[i][3] for i in range(len(all_data)
) 
        return dicts

【问题讨论】:

【参考方案1】:

我不得不承认我没有阅读您的全部代码。但是,如果您想在def building_dict 块中引用您在InstangramCleaningdef __init__() 中分配的任何属性,请使用self.your_attribute 而不是your_attribute。例如:

def building_dict(self):
    allin = preprocessing(self.rawdata)

此外,我实际上建议阅读一些有关 python 类结构的信息:Python Classes 可能会有所帮助。

【讨论】:

以上是关于建立一个类:building_dict() 接受 1 个位置参数,但给出了 2 个的主要内容,如果未能解决你的问题,请参考以下文章

设计程序可从命令行接受多个数字,求和后输出结果。

Spring 随笔05 文件上传

第一次接受信用卡。我需要做啥?

手写服务器Httpserver

python之timeit模块

亿万富翁萨利纳斯希望建立墨西哥第一家接受比特币的银行