I'm having some trouble trying to yield my dictionary in every run of my "for" loop. I have a csv file that I got from ICANN and want to populate the dictionary with the data and yield each unique dictionary but after doing so and print the resulting list, I have the same dictionary 255 times. The strange thing is if I specifically print ip_data["prefix"]
it will print from 000 - 255.
import csv
import re
def populate_dictionary():
line_count = 0
ip_data = {"prefix": "", "designation": "", "status": ""}
ip_data_list = []
with open("ipv4-address-space.csv") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
ip_data["prefix"] = row[0]
ip_data["designation"] = row[1]
ip_data["status"] = row[5]
line_count += 1
yield ip_data
ip_dictionary_list = populate_dictionary()
print(*ip_dictionary_list)
Even if I return a list after the looping rather than yield a dictionary, I have exactly the same issue when printing the list.
Output:
{'prefix': '255/8', 'designation': 'Future use', 'status': 'RESERVED'}
{'prefix': '255/8', 'designation': 'Future use', 'status': 'RESERVED'}
{'prefix': '255/8', 'designation': 'Future use', 'status': 'RESERVED'}
{'prefix': '255/8', 'designation': 'Future use', 'status': 'RESERVED'}
...
...
...
question from:
https://stackoverflow.com/questions/65939069/dictionary-list-populating-the-same-data-255-times 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…