[prog] Problem with objects in Python3

Veronica Berglyd Olsen veronica at berglyd.net
Wed Mar 30 20:22:25 UTC 2022


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.  The
> dictionary is good, because the keys can be referenced by a variable
> without me needing to know what the actual keys are.
>
> My program now references the data in the way I want.  All I have to do
> now 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) according
>> to "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 to
>> excuse me if my syntax is wrong - but that's what you're looking for,
>> I think.
>>
>> Cheers!
>> Isabelle
>> On 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 I
>>> am
>>>
>>> certainly nowhere near being an expert (nor ever likely to be).
>>>
>>>
>>>
>>> The problem I am struggling with at the moment is this: the program
>>> I
>>>
>>> am working on reads a file which contains data about a number of
>>>
>>> people.  Each line has data about just one person (including
>>> her/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 it
>>> is
>>>
>>> derived from data in the input file.  With a hard-coded name I can
>>>
>>> easily check whether the object exists, and create it if it
>>> doesn't;
>>>
>>> but I can't figure out how to do this with names that are derived
>>> from
>>>
>>> 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 list
> Programming at linuxchix.org
> https://mailman.linuxchix.org/mailman/listinfo/programming




More information about the Programming mailing list