Why am I getting duplicate values in my output?
With the following code:
import csv
class AccessPoint(object):
def __init__(self, name, ipv4, model, location):
self.name = name
self.ipv4 = ipv4
self.model = model
self.location = location
print('{0} born'.format(self.name))
def __del__(self):
print('{0} died'.format(self.name))
def main():
ap_list = []
ap_dict = {}
with open('C:\\inventory.csv', 'r') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
ap = AccessPoint(row['Host Name'], row['Static IP Address'],
row['Device Model'], row['Host Name'][0:5])
ap_dict['ap_name'] = ap.name
ap_dict['ap_ipv4'] = ap.ipv4
ap_dict['ap_model'] = ap.model
ap_dict['ap_location'] = ap.location
ap_list.append(ap_dict)
print(ap_list)
if __name__ == '__main__':
main()
When I execute print(ap_list) I get the very last row in the CSV repeated once
for every row in the file. I would expect to see a list of ap_dicts, but I do
not. When I add print(ap_dict) directly after the ap_list.append(ap_dict)
inside the loop I see the correct values. Why is it that the print(ap_list)
that happens outside of the for loop is only printing the last item on repeat?
Here is some example output data:
AP1 born
AP2 born
AP1 died
AP3 born
AP2 died
[{'ap_name': 'AP3', 'ap_ipv4': '192.168.1.1', 'ap_model': 'Linksys',
'ap_location': 'HQ'}, {'ap_name': 'AP3', 'ap_ipv4': '192.168.1.1', 'ap_model':
'Linksys', 'ap_location': 'HQ'}, {'ap_name': 'AP3', 'ap_ipv4': '192.168.1.1',
'ap_model': 'Linksys', 'ap_location': 'HQ'}
AP3 died
Thanks in advance.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting duplicate values in my output?
On Thursday, July 25, 2019 at 9:57:38 PM UTC-5, Chris Angelico wrote:
> On Fri, Jul 26, 2019 at 12:46 PM DT wrote:
> > def main():
> > ap_list = []
> > ap_dict = {}
> >
> > for row in csv_reader:
> > ap_dict['ap_name'] = ap.name
> > ap_dict['ap_ipv4'] = ap.ipv4
> > ap_dict['ap_model'] = ap.model
> > ap_dict['ap_location'] = ap.location
> >
> > ap_list.append(ap_dict)
> >
> > print(ap_list)
> >
> >
> > When I execute print(ap_list) I get the very last row in the CSV repeated
> > once for every row in the file. I would expect to see a list of ap_dicts,
> > but I do not. When I add print(ap_dict) directly after the
> > ap_list.append(ap_dict) inside the loop I see the correct values. Why is it
> > that the print(ap_list) that happens outside of the for loop is only
> > printing the last item on repeat?
> >
>
> You create one dictionary, and then reuse it every time. Appending it
> to a list doesn't change it, which means you keep overwriting the same
> dictionary every time you go through the loop.
>
> Try creating a new dictionary for each row; in fact, you can simplify
> things significantly by using a dict literal (or, more technically,
> "dict display") to create a new dictionary with the four fields
> already set. You could even do that in the same operation as appending
> to the list.
>
> ChrisA
That is interesting to know. I guess I was confused because when I put in a
print(ap_dict) in the for loop, it printed out the contents of the dict
correctly for each pass. I assumed that when I then added that dict into the
list that the list would be appended with that iterations values. I guess I
have a bit to learn about how things are referenced in Python.
Thanks for the hint. I updated my code and it works correctly now.
--
https://mail.python.org/mailman/listinfo/python-list
python and outlook
Hi everyone, Are there any links or sites on how to read outlook mail boxes or address book? -- http://mail.python.org/mailman/listinfo/python-list
ctypes: How to call unexported functions in a dll
Hi, I am using ctypes in python 3 on a WXP machine Loading a dll and using its exported functions works fine. Now I want to use a function in the dll that is not exported. In C this can be done by just casting the address in the dll of that function to an apropriate function pointer and call it (you need to be sure about the address). Can a similar thing be done directly with ctypes? A work around I see is writing in wrapper dll in c that loads the main dll, exports a function that calls the unexported function in the main dll, but I don't find that an elegant solution. Thanks for any hint in the right direction. BR Coert -- http://mail.python.org/mailman/listinfo/python-list
