Python program to sum up the Salary when the Name and Age matches with subsequent rows.
I have a .txt file as per below content with 4 fields as Name, Age, Salary and Date of Salary received
Vijay,35,6000,29-12-2023
Manoj,32,12000,29-7-2023
Vinoth,31,11000,29-12-2023
Vijay,32,10000,29-10-2023
Vijay,32,7000,29-12-2023
Manoj,30,10000,29-11-2023
Vinoth,32,10000,29-10-2023
Vijay,32,10000,29-4-2023
Mano,32,1000,29-12-2023
Vijay,32,3000,29-8-2023
I wish to write a python program to sum up the Salary when the Name and Age matches with subsequent rows. Will you able to do the code ?
Certainly! You can achieve this by reading the contents of the file, parsing each line to extract the relevant information (Name, Age, Salary), and then using a dictionary to store the sum of salaries for each unique combination of Name and Age. Here's a Python program that accomplishes this:
def read_file(file_path):
data = []
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line:
name, age, salary, _ = line.split(',')
data.append({'name': name, 'age': int(age), 'salary': int(salary)})
return data
def sum_salaries(data):
salary_sum = {}
for entry in data:
key = (entry['name'], entry['age'])
if key in salary_sum:
salary_sum[key] += entry['salary']
else:
salary_sum[key] = entry['salary']
return salary_sum
def main():
file_path = 'your_file.txt' # Replace with the path to your file
data = read_file(file_path)
salary_sum = sum_salaries(data)
print("Sum of salaries for each unique combination of Name and Age:")
for key, value in salary_sum.items():
name, age = key
print(f"{name}, {age}: {value}")
if __name__ == "__main__":
main()
Make sure to replace
'your_file.txt'
with the actual path to your file. The program reads the file, parses the data, calculates the sum of salaries for each unique combination of Name and Age, and then prints the results.Nice, I need the salary dates when displaying the result
Certainly! You can modify the program to also store the salary dates for each unique combination of Name and Age. Here's the updated code:
def read_file(file_path):
data = []
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line:
name, age, salary, date = line.split(',')
data.append({'name': name, 'age': int(age), 'salary': int(salary), 'date': date})
return data
def sum_salaries(data):
salary_info = {}
for entry in data:
key = (entry['name'], entry['age'])
if key in salary_info:
salary_info[key]['sum'] += entry['salary']
salary_info[key]['dates'].append(entry['date'])
else:
salary_info[key] = {'sum': entry['salary'], 'dates': [entry['date']]}
return salary_info
def main():
file_path = 'your_file.txt' # Replace with the path to your file
data = read_file(file_path)
salary_info = sum_salaries(data)
print("Sum of salaries with dates for each unique combination of Name and Age:")
for key, value in salary_info.items():
name, age = key
salary_sum = value['sum']
salary_dates = ', '.join(value['dates'])
print(f"{name}, {age}: Sum - {salary_sum}, Dates - {salary_dates}")
if __name__ == "__main__":
main()
This version of the program keeps track of both the sum of salaries and the corresponding salary dates for each unique combination of Name and Age. The result is then printed with the sum and dates included.
Comments
Post a Comment