[prog] Problem with objects in Python3
David Sumbler
david at aeolia.co.uk
Thu Mar 31 12:47:20 UTC 2022
Thanks for all that.
I understand the point about using
A = persons.get("John Doe")
but in fact I used
try:
players[name].name
except KeyError:
players[name] = Person(name)
which also does the trick. (players is the dictionary and Person is
the object type)
I need to end up with a .csv file, so that I can then load it into
LibreOffice and add some formatting. I'll certainly have a look at the
pandas package, but my output file is so simple in format that it's
probably over-kill.
Thanks again
David
On Wed, 2022-03-30 at 22:22 +0200, Veronica Berglyd Olsen wrote:
> Hi,
> I'm a Python developer, and was just typing up what Isabelle here
> already proposed when this email arrived. It's precisely the solution
> for Python. Syntax is also fine (good memory!), although no need for
> parenthesis in the if-statement.
> Since I was already working on a response, I'll send the rest. For
> instance that you don't need an if-statement if you just want to
> extract data from the dictionary. You can also use:
> A = persons.get("John Doe")
> This will return the object if the dictionary contains it, and None
> if it doesn't (or you can specify another default value as the second
> argument). This call doesn't raise a KeyError on a missing key, which
> is convenient.
> You can also loop over entries like this:
> for key, value in persons.items(): # ... process data
> You may also consider using a dictionary for the Person object as
> well, with the data as key/value pairs. This makes it significantly
> simpler to write them to a file. For instance JSON files can be
> written:
> import json
> with open("filename.json", mode="w") as f: json.dump(persons, f,
> indent=2)
> See:
> https://docs.python.org/3/library/json.html?highlight=json%20dump#json.dump
>
> Or if you need a CSV file you can use the third party pandas package.
> - Veronica
>
> On 30/03/2022 22:05, David Sumbler wrote:
> > Thanks for your swift reply. And yes, that did the
> > trick. Thedictionary is good, because the keys can be referenced
> > by a variablewithout me needing to know what the actual keys are.
> > My program now references the data in the way I want. All I have
> > to donow is process the data in a suitable manner and write an
> > output file.
> > Thank you for your help.
> > David
> >
> > On Wed, 2022-03-30 at 20:56 +0200, Isabelle Hurbain-Palatin wrote:
> > > Hi David,You're probably looking for a dictionary -
> > > https://docs.python.org/3/library/stdtypes.html#typesmapping
> > > It allows you to store "content" (your Person data object)
> > > accordingto "keys" (that person's name) with syntax such as
> > > persons = {}persons['John Doe'] = Person(....)if ('John Doe' in
> > > persons): doStuff()
> > > I haven't coded in Python in quite some time, so you'll have
> > > toexcuse me if my syntax is wrong - but that's what you're
> > > looking for,I think.
> > > Cheers!IsabelleOn Wed, Mar 30, 2022 at 8:45 PM David Sumbler <
> > > david at aeolia.co.uk>wrote:
> > > > I have used Python quite a bit for odd projects in the past,
> > > > but Iam
> > > > certainly nowhere near being an expert (nor ever likely to be).
> > > >
> > > >
> > > > The problem I am struggling with at the moment is this: the
> > > > programI
> > > > am working on reads a file which contains data about a number
> > > > of
> > > > people. Each line has data about just one person
> > > > (includingher/his
> > > > name), and normally there will be a number of other lines with
> > > > different data about that same person.
> > > >
> > > >
> > > > What I want to do is to use the person's name (with spaces
> > > > removed)as
> > > > a variable name, and create a Person object using that name.
> > > >
> > > >
> > > > My problem is that I can't hard code the variable name because
> > > > itis
> > > > derived from data in the input file. With a hard-coded name I
> > > > can
> > > > easily check whether the object exists, and create it if
> > > > itdoesn't;
> > > > but I can't figure out how to do this with names that are
> > > > derivedfrom
> > > > data in a file.
> > > >
> > > >
> > > > Can somebody point me in the right direction?
> > > >
> > > >
> > > > David
> > > > _______________________________________________
> > > > Programming mailing list
> > > > Programming at linuxchix.org
> > > >
> > > > https://mailman.linuxchix.org/mailman/listinfo/programming
> > > >
> >
> > _______________________________________________Programming mailing
> > listProgramming at linuxchix.org
> > https://mailman.linuxchix.org/mailman/listinfo/programming
>
> _______________________________________________Programming mailing
> listProgramming at linuxchix.org
> https://mailman.linuxchix.org/mailman/listinfo/programming
More information about the Programming
mailing list