Python - creating multiple objects for a class - python

In python I need to create 43 instances of a class 'Student' that includes the variables first_name, middle_name, last_name, student_id by reading in a file (Students.txt) and parsing it. The text file appears like this:
Last Name Midle Name First Name Student ID
----------------------------------------------
Howard Moe howar1m
Howard Curly howar1c
Fine Lary fine1l
Howard Shemp howar1s
Besser Joe besse1j
DeRita Joe Curly derit1cj
Tiure Desilijic Jaba tiure1jd
Tharen Bria thare1b
Tai Besadii Durga tai1db
Hego Damask hego1d
Lannister Tyrion lanni1t
Stark Arya stark1a
Clegane Sandor clega1s
Targaryen Daenerys targa1d
Bombadil Tom bomba1t
Brandybuck Meriadoc brand1m
Took Pregrin took1p
McCoy Leonard mccoy1l
Scott Montgomery scott1m
Crusher Wesley crush1w
Montoya Inigo monto1i
Rugen Tyrone rugen1t
Solo Han solo1h
Corey Carl corey1c
Flaumel Evelyn flaum1e
Taltos Vlad talto1v
e'Drien Morrolan edrie1m
Watson John watso1j
McCoy Ebenezar mccoy1e
Carpenter Molly carpe1m
Graystone Zoe grays1z
Adama William adama1w
Adama Joseph Leland adama1l
Roslin Laura rosli1l
Baltar Gaius balta1g
Tigh Ellen tigh1e
Tigh Saul tigh1s
Cottle Sherman cottl1s
Zarek Thomas zarek1t
Murphy James Alexander murph1a
Sobchak Walter sobch1w
Dane Alexander dane1a
Gruber Hans grube1h
Biggs John Gil biggs1gj
The class student is:
class Student (object):
def __init__(self, first_name, middle_name, last_name, student_id):
self.__first_name = first_name
self.__middle_name = middle_name
self.__last_name = last_name
self.__student_id = student_id
What would be the easiest way to read into 'Students.txt' and create each instance of student?

Step by step tutorial
To read the file content, use io.open. Don't forget to specify the file encoding if any name has accentuated characters.
with io.open('students.txt', mode="r", encoding="utf8") as fd:
content = fd.read()
Here, you read the whole content and store it in memory (amount of data is small). You can also use an iterator.
Then, you can split the content line by line with str.splitlines():
lines = content.splitlines()
# print(lines)
You get something like:
['Last Name Midle Name First Name Student ID ',
'----------------------------------------------',
'Howard Moe howar1m ',
'Howard Curly howar1c ',
'Fine Lary fine1l ',
'Howard Shemp howar1s ',
'Besser Joe besse1j ',
'DeRita Joe Curly derit1cj ',
'Tiure Desilijic Jaba tiure1jd ',
'Tharen Bria thare1b ']
You have (nearly) fixed-length lines, so you can use slices to extract the fields.
Here is what you can do for the header:
header = lines.pop(0)
fields = header[0:8], header[11:21], header[23:33], header[36:46]
# print(fields)
You get:
('Last Nam', 'Midle Name', 'First Name', 'Student ID')
You can drop the line of hyphens:
lines.pop(0)
For each line, you can extract values using slices too. Note: slice indices are slightly different:
for line in lines:
record = line[0:8], line[12:21], line[23:34], line[36:46]
# print(record)
You'll get values with trailing space:
('Howard ', ' ', ' Moe ', 'howar1m ')
('Howard ', ' ', ' Curly ', 'howar1c ')
('Fine ', ' ', ' Lary ', 'fine1l ')
('Howard ', ' ', ' Shemp ', 'howar1s ')
('Besser ', ' ', ' Joe ', 'besse1j ')
('DeRita ', 'Joe ', ' Curly ', 'derit1cj ')
('Tiure ', 'Desilijic', ' Jaba ', 'tiure1jd ')
('Tharen ', ' ', ' Bria ', 'thare1b ')
To avoid trailing spaces, use str.strip() function:
for line in lines:
record = line[0:8], line[12:21], line[23:34], line[36:46]
record = [v.strip() for v in record]
# print(record)
You get:
['Howard', '', 'Moe', 'howar1m']
['Howard', '', 'Curly', 'howar1c']
['Fine', '', 'Lary', 'fine1l']
['Howard', '', 'Shemp', 'howar1s']
['Besser', '', 'Joe', 'besse1j']
['DeRita', 'Joe', 'Curly', 'derit1cj']
['Tiure', 'Desilijic', 'Jaba', 'tiure1jd']
['Tharen', '', 'Bria', 'thare1b']
At this point, I recommend you to store your record as a dict in a list:
records = []
for line in lines:
record = line[0:8], line[12:21], line[23:34], line[36:46]
record = [v.strip() for v in record]
records.append(dict(zip(header, record)))
You get:
[{'First Name': 'Moe', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1m'},
{'First Name': 'Curly', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1c'},
{'First Name': 'Lary', 'Last Nam': 'Fine', 'Midle Name': '', 'Student ID': 'fine1l'},
{'First Name': 'Shemp', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1s'},
{'First Name': 'Joe', 'Last Nam': 'Besser', 'Midle Name': '', 'Student ID': 'besse1j'},
{'First Name': 'Curly', 'Last Nam': 'DeRita', 'Midle Name': 'Joe', 'Student ID': 'derit1cj'},
{'First Name': 'Jaba', 'Last Nam': 'Tiure', 'Midle Name': 'Desilijic', 'Student ID': 'tiure1jd'},
{'First Name': 'Bria', 'Last Nam': 'Tharen', 'Midle Name': '', 'Student ID': 'thare1b'}]
But you can also use a class:
class Student(object):
def __init__(self, first_name, middle_name, last_name, student_id):
self.first_name = first_name
self.middle_name = middle_name
self.last_name = last_name
self.student_id = student_id
def __repr__(self):
fmt = "<Student('{first_name}', '{middle_name}', '{last_name}', '{student_id}')>"
return fmt.format(first_name=self.first_name, middle_name=self.middle_name, last_name=self.last_name, student_id=self.student_id)
And construct a list of students:
students = []
for line in lines:
record = line[0:8], line[12:21], line[23:34], line[36:46]
record = [v.strip() for v in record]
students.append(Student(*record))
You get:
[<Student('Howard', '', 'Moe', 'howar1m')>,
<Student('Howard', '', 'Curly', 'howar1c')>,
<Student('Fine', '', 'Lary', 'fine1l')>,
<Student('Howard', '', 'Shemp', 'howar1s')>,
<Student('Besser', '', 'Joe', 'besse1j')>,
<Student('DeRita', 'Joe', 'Curly', 'derit1cj')>,
<Student('Tiure', 'Desilijic', 'Jaba', 'tiure1jd')>,
<Student('Tharen', '', 'Bria', 'thare1b')>]

list_of_students = []
with open('students.txt') as f:
for line in f:
data = line.split()
if len(data) == 3:
firstname, lastname, id = data
list_of_students.append(Student(firstname, '', lastname, id))
elif len(data) == 4:
list_of_students.append(Student(*data))
else:
raise ValueError
I'm not usre exactly how your input file is laid out, so there's a little processing here to handle the cases where there is no middle name.

Related

Creating nested dictionary function using text file in Python

I'm trying to create a nested if function with the username being the main string and the others being the substring. But for some reason, it does not separate the lines, creating multiple nested dictionaries instead right now it's just stuck in the first line of the text file. Plz help
MY TEXT FILE
shanm = null|Shanmugaraja|09/04/2002|0149606345|020409140817|0102393345|4770 4848 0109 0294
jiken = null|Soo Jiken|08/06/2000|0149600239|020908140213|011349780|8900 2828 1129 0889
MY CODE FOR NESTED DICTIONARY
with open("something.txt", 'r') as f:
data_dict = {}
data_dict2 = {}
data3 = {}
for line in f:
f.read()
k, v = line.strip().split("=")
listDetails = line.strip().split('|')
data_dict = {"Full Name": listDetails[1]}
data_dict.update({"Date of Birth": listDetails[2]})
data_dict.update({"Phone Number": listDetails[3]})
data_dict.update({"Identification Card (IC) Number": listDetails[4]})
data_dict.update({"Emergency Contact Number": listDetails[5]})
data_dict.update({"Credit /Debit Card Details ": listDetails[6]})
data3[k] = data_dict
print(data3)
DESIRED OUTPUT
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 4848 0109 0294'}}
{'jiken ': {'Full Name': 'Soo Ji', 'Date of Birth': '08/06/2000', 'Phone Number': '0149600239', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
OUTPUT RECEIVED
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 4848 0109 0294'}}
#NOT READING THE NEXT PART OF THE LINE
OR (WITHOUT "F.READ()"
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 2828 0109 0394'}}
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 2828 0109 0394'}, 'jiken ': {'Full Name': 'Soo Jiken', 'Date of Birth': '08/06/2000', 'Phone Number': '0149600239', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
READS EVERYTHING ALL OVER AGAIN
try updating your code to this:
with open("something.txt", 'r') as f:
data_dict = {}
data3 = {}
for line in f.readlines():
k, v = line.strip().split("=")
listDetails = line.strip().split('|')
data_dict = {"Full Name": listDetails[1]}
data_dict.update({"Date of Birth": listDetails[2]})
data_dict.update({"Phone Number": listDetails[3]})
data_dict.update({"Identification Card (IC) Number": listDetails[4]})
data_dict.update({"Emergency Contact Number": listDetails[5]})
data_dict.update({"Credit /Debit Card Details ": listDetails[6]})
data3[k] = data_dict
print(data3)
Looks like you want a list of dictionaries. If so, you could do it like this:
keys = ['Full name', 'Date of Birth', 'Phone Number',
'Identification Card (IC) Number', 'Emergency Contact Number', 'Credit /Debit Card Details']
lod = []
with open('something.txt') as infile:
for line in infile:
key, v = line.split('=')
d = {}
for k, v in zip(keys, v.split('|')[1:]):
d[k] = v
lod.append({key.strip(): d})
for d in lod:
name = list(d.keys())[0]
print(d[name]['Full name']
keys = ["Full Name", "Date of Birth", "Phone Number",
"Identification Card (IC) Number", "Emergency Contact Number",
"Credit /Debit Card Details "]
data = {}
with open("something.txt", 'r') as f:
for line in f:
key, values = line.strip().split(" = ") # note the space around =, to avoid trailing space in key
values = values.split('|')
data[key] = dict(zip(keys, values[1:]))}
print(data)
print(data.get('jiken'))
Probably you want to store each dict data in some sort of container type like list.
UPDATE: I edited my code to create a dict and access each record by username.

python else part in if runs many times , how to solve

here is my code for showing search record and showing inform usr if found nothing.
Problem: else part runs as many times as outer loop.
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
print(search)
for person in entries:
# print(person)
if person["Last Name"] == search:
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
else:
print("There is no record found as you search Keyword")
thats because each iteration you are checking only 1 person, and if you didn't find what you looked for, you are printing that it does not exist.
this is actually an undesired behavior.
a better solution would be to simply look in the set of values you need:
...
search = input("type your search: ")
founds = [entry for entry in entries if entry["Last Name"] == search)] ## filtering only records that match what we need using list comprehension
if founds:
for found in founds:
* print info *
else:
print("There is no record found as you search Keyword")
First, check if the Last Name that the user enters is present in the dictionaries. If yes, then loop through them and print the respective records. Else, display no records found. Here is how you do it:
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
print(search)
if search in [person['Last Name'] for person in entries]:
for person in entries:
if person["Last Name"] == search:
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
else:
print("There is no record found as you search Keyword")
Output:
type your search: >? Khan
Khan
Here are the records found for your search
First Name : Sher
Last Name : Khan
Age : 22
Telephone : 2989484
Here are the records found for your search
First Name : Ali
Last Name : Khan
Age : 22
Telephone : 398439
Here are the records found for your search
First Name : Talha
Last Name : Khan
Age : 22
Telephone : 3343434
type your search: >? Jones
Jones
There is no record found as you search Keyword
Try like this (Use a boolean Found variable)
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'},
{'First Name': 'Talha', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
found = False
print(search)
for person in entries:
if person["Last Name"] == search:
found = True
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
if not found:
print("There is no record found as you search Keyword")
This might not be the best way of doing it but this would work I think
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone':
'2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone':
'3343434'}]
inEntries = False
search = input("type your search: ")
print(search)
for person in entries:
# print(person)
if person["Last Name"] == search:
inEntries = True
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
if not inEntries:
print("There is no record found as you search Keyword")

Python remove newlines from a column in csv file

I tried lot of suggestions but I am unable to remove carriage returns. I am new python and trying it with csv file cleaning.
import csv
filepath_i = 'C:\Source Files\Data Source\Flat File Source\PatientRecords.csv'
filepath_o = 'C:\Source Files\Data Source\Flat File Source\PatientRecords2.csv'
rows = []
with open(filepath_i, 'rU', newline='') as csv_file:
#filtered = (line.replace('\r\n', '') for line in csv_file)
filtered = (line.replace('\r', '') for line in csv_file)
csv_reader = csv.reader(csv_file, delimiter=',')
i = 0
for row in csv_reader:
print(row)
i = i + 1
if(i == 10):
break
#with open(filepath_o, 'w',newline='' ) as writeFile:
# writer = csv.writer(writeFile,lineterminator='\r')
# for row in csv_reader:
# #rows.append(row.strip())
# rows.append(row.strip())
# writer.writerows(rows)
Input
DRG Definition,Provider Id,Provider Name,Provider Street Address,Provider City,Provider State,Provider Zip Code,Hospital Referral Region Description,Hospital Category,Hospital Type, Total Discharges ,Covered Charges , Total Payments ,Medicare Payments
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10001,SOUTHEAST ALABAMA MEDICAL CENTER,1108 ROSS CLARK CIRCLE,DOTHAN,AL,36301,AL - Dothan,Specialty Centers,Government Funded,91,"$32,963.07 ","$5,777.24 ","$4,763.73 "
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10005,MARSHALL MEDICAL CENTER SOUTH,"2505 U S HIGHWAY
431 NORTH",BOAZ,AL,35957,AL - Birmingham,Specialty Centers,Private Institution,14,"$15,131.85 ","$5,787.57 ","$4,976.71 "
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10006,ELIZA COFFEE MEMORIAL HOSPITAL,205 MARENGO STREET,FLORENCE,AL,35631,AL - Birmingham,Rehabilitation Centers,Private Institution,24,"$37,560.37 ","$5,434.95 ","$4,453.79 "
Output (4th column 'Provider Street Address')
['DRG Definition', 'Provider Id', 'Provider Name', 'Provider Street Address', 'Provider City', 'Provider State', 'Provider Zip Code', 'Hospital Referral Region Description', 'Hospital Category', 'Hospital Type', ' Total Discharges ', 'Covered Charges ', ' Total Payments ', 'Medicare Payments']
['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10001', 'SOUTHEAST ALABAMA MEDICAL CENTER', '1108 ROSS CLARK CIRCLE', 'DOTHAN', 'AL', '36301', 'AL - Dothan', 'Specialty Centers', 'Government Funded', '91', '$32,963.07 ', '$5,777.24 ', '$4,763.73 ']
['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10005', 'MARSHALL MEDICAL CENTER SOUTH', '2505 U S HIGHWAY \n431 NORTH', 'BOAZ', 'AL', '35957', 'AL - Birmingham', 'Specialty Centers', 'Private Institution', '14', '$15,131.85 ', '$5,787.57 ', '$4,976.71 ']
I ran this on my side and it works:
with open(filepath_i, 'rU', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
row[3] = row[3].replace("\n","").replace("\r","")
print(row)
Output:
['DRG Definition', 'Provider Id', 'Provider Name', 'Provider Street Address', 'Provider City', 'Provider State', 'Provider Zip Code', 'Hospital Referral Region Description', 'Hospital Category', 'Hospital Type', ' Total Discharges ', 'Covered Charges ', ' Total Payments ', 'Medicare Payments']
['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10001', 'SOUTHEAST ALABAMA MEDICAL CENTER', '1108 ROSS CLARK CIRCLE', 'DOTHAN', 'AL', '36301', 'AL - Dothan', 'Specialty Centers', 'Government Funded', '91', '$32,963.07 ', '$5,777.24 ', '$4,763.73 ']
['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10005', 'MARSHALL MEDICAL CENTER SOUTH', '2505 U S HIGHWAY 431 NORTH', 'BOAZ', 'AL', '35957', 'AL - Birmingham', 'Specialty Centers', 'Private Institution', '14', '$15,131.85 ', '$5,787.57 ', '$4,976.71 ']
['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10006', 'ELIZA COFFEE MEMORIAL HOSPITAL', '205 MARENGO STREET', 'FLORENCE', 'AL', '35631', 'AL - Birmingham', 'Rehabilitation Centers', 'Private Institution', '24', '$37,560.37 ', '$5,434.95 ', '$4,453.79 ']

Wrong output in appending items to a dictionary of dictionary in python

I have this empty dictionary of dictionary in python and I am trying to append/add items to it. The individual inner dictionary itself has list as its value for key.
The empty structure of my dict of dict is:
grp_task_text_dict = {'Group 1': {}, 'Group 2': {}}
Here is my current code which reads items and based on certain conditions, it needs to append that item to either Group 1 or Group 2:
def text_mapping_bot2():
group_list = ['Group 1','Group 2']
task_text_dict = {}
grp_task_text_dict = {i: task_text_dict for i in group_list}
grp_mapping = {i: [] for i in group_list}
print('EMPTY GROUP TASK TEXT DICT = {}'.format(grp_task_text_dict))
bot_mapping = [{'Name': '', 'Task ID': '0','Task Text': 'Try again!', 'Group': '', 'Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '1.0','Task Text': 'Hi, Welcome', 'Group': 'Group 2', 'Complementary Action': 'group'}, {'Name': 'Hello', 'Task ID': '11.0', 'Task Text': 'Tell me about car options? ', 'Group': 'Group 2', 'Complementary Action': ''}, {'Bot Name': 'Hello', 'Task ID': '11.0', 'Task Text': 'What are different car options?', 'Group': 'Group 2', 'Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '11.0','Task Text': 'What cars are there?', 'Group': 'Group 2','Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '11.0','Task Text': 'May I know about Car types?', 'Group': 'Group 2','Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '2.0', 'Task Text': 'How much is the rent of compact car? ', 'Group': 'Group 2','Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '2.0','Task Text': 'Compact car expenses?', 'Group': 'Group 2', 'Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '2.0', 'Task Text': 'Cost of compact car?', 'Group': 'Group 2', 'Complementary Action': ''}, {'Name': 'Hello', 'Task ID': '2.0','Task Text': 'How much do I need to pay for small car', 'Group': 'Group 2', 'Complementary Action': ''}]
for item in bot_mapping:
print("ITEM IN BOT MAPPING = {}".format(item))
print('\n')
print('\n')
for grp, val in grp_task_text_dict.items():
print('CURRENT VAL = {} and GROUP = {}'.format(val, grp))
try:
if last_id < int(float(item['Task ID'])):
last_id = int(float(item['Task ID']))
except:
pass
try:
if item['Task ID'] == '':
item['Task ID'] = '0'
if (item['Task ID'] in val) and (str(item['Group']).lower() == str(grp).lower()):
print('\n')
print('CURRENT STATUS OF GROUP TASK TEXT DICT BEFORE APPENDING= {}'.format(
grp_task_text_dict))
print('\n')
print('GROUP AND TASK ID ALREADY IN ITEM AND TASK TEXT DICT IS SAME - {} and {}'.format(
str(item['Group']).lower(), str(grp).lower()))
print('\n')
print('APPENDING TO THIS GROUP IN GRP TASK DICT = {}'.format(grp_task_text_dict[grp][item['Task ID']]))
# val[item['Task ID']].append(item['Task Text'])
grp_task_text_dict[grp][item['Task ID']].append(item['Task Text'])
print('CURRENT STATUS OF GROUP TASK TEXT DICT AFTER APPENDING= {}. Going to Break out of loop'.format(
grp_task_text_dict))
break
elif str(item['Group']).lower() == str(grp).lower():
print('CURRENT STATUS OF GROUP TASK TEXT DICT BEFORE APPENDING IN NEW TASK ID= {}'.format(
grp_task_text_dict))
print('\n')
print('NEW TASK ID BUT GROUP SAME = {} and {}'.format(str(item['Group']).lower(), str(grp).lower()))
print('\n')
print('APPENDING TO THIS GROUP IN GRP TASK DICT IN NEW ID = {}'.format(grp_task_text_dict[grp]))
print('\n')
# val[item['Task ID']] = [item['Task Text']]
grp_task_text_dict[grp][item['Task ID']] = [item['Task Text']]
print(
'CURRENT STATUS OF GROUP TASK TEXT DICT AFTER APPENDING IN NEW TASK ID = {}. Going to Break out of loop'.format(
grp_task_text_dict))
break
except Exception as e:
print("THIS EXCEPTION CAME = {}".format(e))
# print('--iteration check--', task_text_dict)
What I want is that based on Group filed in each item in bot_mapping, the code should either append it to the list associated with Task ID of that group dictionary in grp_task_text_dict if the Task ID is already existing in that group dictionary, or create a new list with new Task ID within the matching group dictionary in grp_task_text_dict. So for example if Group in item is Group 2 and Task ID is 11.0 then this item should be appended to list of Task ID 11.0 in Group 2 dictionary of grp_task_text_dict and not in Group 1.
When I execute this code it appends the item to both Group 1 and Group 2 dictionaries even though my code is appending to specific group using grp_task_text_dict[grp][item['Task ID']].append(item['Task Text']) after matching the Group name using if (item['Task ID'] in val) and (str(item['Group']).lower() == str(grp).lower()):.
A sample output of my code looks like this:
ITEM IN BOT MAPPING = {'Bot Name': 'Hello', 'Task ID': '11.0', 'Task Text': 'What are different car options?', 'Group': 'Group 2', 'Complementary Action': ''}
CURRENT VAL = {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ']} and GROUP = Group 1
CURRENT VAL = {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ']} and GROUP = Group 2
CURRENT STATUS OF GROUP TASK TEXT DICT BEFORE APPENDING= {'Group 1': {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ']}, 'Group 2': {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ']}}
GROUP AND TASK ID ALREADY IN ITEM AND TASK TEXT DICT IS SAME - group 2 and group 2
APPENDING TO THIS GROUP IN GRP TASK DICT = ['Tell me about car options? ']
CURRENT STATUS OF GROUP TASK TEXT DICT AFTER APPENDING= {'Group 1': {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ', 'What are different car options?']}, 'Group 2': {'1.0': ['Hi, Welcome'], '11.0': ['Tell me about car options? ', 'What are different car options?']}}
As you can see above it is appending to both Group 1 and Group 2 when it should only append to Group 2.
I know there is some error in my logic but I am unable to figure out what and where the issue is which is causing this wrong output. Any help?
The problem is this line:
grp_task_text_dict = {i: task_text_dict for i in group_list}
This makes all the elements in this dictionary refer to the same task_text_dict dictionary, they don't get copies. Change it to:
grp_task_text_dict = {i: {} for i in group_list}
Then it will initialize each dictionary element with its own empty dictionary.

Create a key in a nested dict without using update function

Input :
{'Name': 'A','Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M','Phone Number': '01234567', 'Mobile Number': '9876543210', 'Date of Birth': '01-01-95'}
1.
d.update({'Contact Info': {'Mobile Number':d['Mobile Number'],'Phone
Number':d['Phone Number'] }})
2.
d['Contact Info']={}
d['Contact Info']['Mobile Number']=d['Mobile Number']
Can you say any better way or different way to create a dictionary key which can be assigned to a dict item as value???
Original Code:
import csv
import copy
from collections import namedtuple
d={}
ls=[]
def nest():
with open ("details.csv", 'r') as f:
reader=csv.DictReader(f)
for row in reader:
d.update(row)
PersonalDetails = namedtuple('PersonalDetails','blood_group age sex')
ContactInfo = namedtuple('ContactInfo','phone_number mobile_number')
d1=copy.deepcopy(d)
ls.append(d1)
print ls
nest()
This is how I would update my dict of dicts:
I would create a function that will take a 3 arguments(The key of the subdict, the subkey of said subdict and the value you want to change.) I assign to be updated and then update that value.
d = {
'Name': 'A',
'Personal Details': {'Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M'},
'Contact Info': {'Phone Number': '01234567', 'Mobile Number': '9876543210'},
'Date of Birth': '01-01-95'
}
def updateInfo(toBeUpdated, subkey, ValueToUpdate):
if toBeUpdated in d:
tempdict = d[toBeUpdated]
tempdict[subkey] = ValueToUpdate
d[toBeUpdated] = tempdict
print (d)
else:
print ("No %s to update" % (toBeUpdated))
updateInfo('Contact Info','Mobile Number','999 999 9999')
the result I get from this:
{'Name': 'A', 'Personal Details': {'Blood Group': 'O +ve', 'Age': '1', 'Sex': 'M'}, 'Contact Info': {'Phone Number': '01234567', 'Mobile Number': '999 999 9999'}, 'Date of Birth': '01-01-95'}

Categories

Resources