简体   繁体   中英

__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. 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? Below is an example of the output I get.

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.

What am I missing?

Thanks.

str returning one giant string

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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