(Closed) Python CSV: (Master and Detail) Search and Insert Value From Detail File into Specific Column On Master File
Question:
I am new in Python actually ):
I have task regarding Python and CSV where I need to add value into specific column on Master file after getting data from Detail file.
Let see sample Master File, Detail File and Output expected.
Master File:
Detail File:
Output Expected:
I have a few source-code to run it but not complete on this stage.
Let See what I have below:
from csv import DictReader
from collections import defaultdict
loaded = defaultdict(list)
month1=[]
month2=[]
month3=[]
def getdetailpayment(data):
f=open(data)
csv_file = csv.DictReader(f, delimiter=",")
for row in csv_file:
print(dict(row))
f.close()
def search_masterfile(data):
word = input("Search name: ")
f=open(data)
my_reader=csv.DictReader(f,delimiter=",")
for row in my_reader:
for entry in row:
if row[entry]==word:
print(row)
#insert value into this row on specific column
f.close()
search_masterfile("csv/master.csv")
getdetailpayment("csv/detail.csv")
My plan to playing with dictionary where I assumed I can insert into Master file value and specific column based Detail file record existing. Unfortunately I am very weak of knowledge regarding this scope and I already try to get source-code from google but still not what I wants.
Please help me guys regarding this matter and I prompt thank you very munch on advance.
Answers:
If you want to do this with basic Python you could try the following:
import csv
from datetime import datetime as dt
with open("master.csv", "r") as file:
reader = csv.DictReader(file)
fieldnames = reader.fieldnames
master = {(row["Name"], row["Key No"]): row for row in reader}
def to_month(string):
return f"Month {dt.strptime(string, '%b-%y').month}"
with open("details.csv", "r") as file:
next(file)
for name, key, month, amount in csv.reader(file):
master[name, key][to_month(month)] = amount
with open("result.csv", "w") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(master.values())
- The first step reads the master file (here
master.csv
) into a dictionary master
with (Name, Key No)
tuples as keys (for the merging). On the way the column names get picked up (fieldenames
) for the writing later.
- The second step is reading through the details file (here
details.csv
) and updating the respective values in master
. The to_month
function transforms the month-strings into the corresponding key (column name).
- Then write the result into a new file (here
result.csv
).
You might have to adjust the filenames.
The result for the given input
master.csv
:
Name,Key No,Month 1,Month 2,Month 3
A,1,,,
B,2,,,
C,3,,,
D,4,,,
details.csv
:
Name,Key No,Month,Amount
A,1,Feb-22,100
B,2,Jan-22,80
C,3,Feb-22,80
D,4,Jan-22,100
A,1,Jan-22,200
C,3,Jan-22,90
is the following file result.csv
:
Name,Key No,Month 1,Month 2,Month 3
A,1,200,100,
B,2,80,,
C,3,90,80,
D,4,100,,
If you have to do stuff like this often you might want to look into the Pandas library.
I am new in Python actually ):
I have task regarding Python and CSV where I need to add value into specific column on Master file after getting data from Detail file.
Let see sample Master File, Detail File and Output expected.
Master File:
Detail File:
Output Expected:
I have a few source-code to run it but not complete on this stage.
Let See what I have below:
from csv import DictReader
from collections import defaultdict
loaded = defaultdict(list)
month1=[]
month2=[]
month3=[]
def getdetailpayment(data):
f=open(data)
csv_file = csv.DictReader(f, delimiter=",")
for row in csv_file:
print(dict(row))
f.close()
def search_masterfile(data):
word = input("Search name: ")
f=open(data)
my_reader=csv.DictReader(f,delimiter=",")
for row in my_reader:
for entry in row:
if row[entry]==word:
print(row)
#insert value into this row on specific column
f.close()
search_masterfile("csv/master.csv")
getdetailpayment("csv/detail.csv")
My plan to playing with dictionary where I assumed I can insert into Master file value and specific column based Detail file record existing. Unfortunately I am very weak of knowledge regarding this scope and I already try to get source-code from google but still not what I wants.
Please help me guys regarding this matter and I prompt thank you very munch on advance.
If you want to do this with basic Python you could try the following:
import csv
from datetime import datetime as dt
with open("master.csv", "r") as file:
reader = csv.DictReader(file)
fieldnames = reader.fieldnames
master = {(row["Name"], row["Key No"]): row for row in reader}
def to_month(string):
return f"Month {dt.strptime(string, '%b-%y').month}"
with open("details.csv", "r") as file:
next(file)
for name, key, month, amount in csv.reader(file):
master[name, key][to_month(month)] = amount
with open("result.csv", "w") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(master.values())
- The first step reads the master file (here
master.csv
) into a dictionarymaster
with(Name, Key No)
tuples as keys (for the merging). On the way the column names get picked up (fieldenames
) for the writing later. - The second step is reading through the details file (here
details.csv
) and updating the respective values inmaster
. Theto_month
function transforms the month-strings into the corresponding key (column name). - Then write the result into a new file (here
result.csv
).
You might have to adjust the filenames.
The result for the given input
master.csv
:Name,Key No,Month 1,Month 2,Month 3 A,1,,, B,2,,, C,3,,, D,4,,,
details.csv
:Name,Key No,Month,Amount A,1,Feb-22,100 B,2,Jan-22,80 C,3,Feb-22,80 D,4,Jan-22,100 A,1,Jan-22,200 C,3,Jan-22,90
is the following file result.csv
:
Name,Key No,Month 1,Month 2,Month 3
A,1,200,100,
B,2,80,,
C,3,90,80,
D,4,100,,
If you have to do stuff like this often you might want to look into the Pandas library.