简体   繁体   English

__str__ 返回一个巨大的字符串而不是单个值。 Python 的新手

[英]__str__ returning one giant string instead of individual values. New to Python

class CovidRecord:

def __init__(self, pruid, prname, prnameFR, date, update, numcomf, numprob, numdeaths, numtotal):
    self.pruid = pruid
    self.prname = prname
    self.prnameFR = prnameFR
    self.date = date
    self.update = update
    self.numconf = numcomf
    self.numprob = numprob
    self.numdeaths = numdeaths
    self.numtotal = numtotal

def __str__(self):
    return self.pruid + self.prname + self.prnameFR + self.date + self.update + self.numconf + self.numprob + self.numdeaths + self.numtotal`

def write_to_dataframe(self):

    dataframe = pd.read_csv(r'C:\Users\jakev\Downloads\covid19-download.csv')
    dataframetop = dataframe.head(6)

    print('Please insert information to add')
    pruid = input('Province ID: ')
    prname = input('Province name: ')
    prnameFR = input('Province name in French: ')
    date = input('Date: ')
    update = input('When was it updated: ')
    numcomf = input('Confirmed cases: ')
    numprob = input('Probable cases: ')
    numdeaths = input('Number of deaths ')
    numtotal = input('Total number')
    newrecord = CovidRecord(pruid, prname, prnameFR, date, update, numcomf, numprob, numdeaths, numtotal)

    dfobj = pd.DataFrame(newrecord, columns = ['prvid', 'prname', 'prnameFR', 'date', 'update', 'numcomf', 'numprob'
        , 'numdeaths', 'numtotal', 'newrecord'], index=['1'])

    newdataframe = dataframetop.append(dfobj, ignore_index = True)

    print(newdataframe)

CovidRecord is a record object I've created to load additional records into a dataframe. CovidRecord 是我创建的记录 object 以将其他记录加载到 dataframe 中。 When I input the responses to the inputs in the terminal, it prints out a giant String with each of my responses under every single column instead of separating them into their individual rightful columns.当我在终端中输入对输入的响应时,它会在每一列下打印出一个巨大的字符串,其中包含我的每个响应,而不是将它们分成各自的合法列。 How can I make it so that when I enter the "prvname" it puts my answer only under province name and so on?我怎样才能做到,当我输入“prvname”时,它只将我的答案放在省名下等等? Below is an example of the output I get.下面是我得到的 output 的示例。

pruid                                       prname                                     prnameFR                                         date 
0   35.0                                      Ontario                                      Ontario                                   2020-01-31  
1   59.0                             British Columbia                         Colombie-Britannique                                   2020-01-31 
6    NaN  23OntarioOntario2021-02-182021-02-182334278  23OntarioOntario2021-02-182021-02-182334278  23OntarioOntario2021-02-182021-02-182334278

the bottom record being the one I attempted to insert.底部记录是我尝试插入的记录。 It taking all my answers to each input, putting them into 1 String and then putting that one giant string into each column.它将我对每个输入的所有答案都放入 1 个字符串中,然后将一个巨大的字符串放入每一列。

What am I missing?我错过了什么?

Thanks.谢谢。

str returning one giant string str返回一个巨大的字符串

You are putting them together yourself with following line:您自己将它们与以下行放在一起:

return self.pruid + self.prname + self.prnameFR + self.date + self.update + self.numconf + self.numprob + self.numdeaths + self.numtotal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM