My csv file ("challenges.csv") contains multiple rows as shown below (the number of columns are different, about 8000 rows):
2937 ,58462bc9a559fa7d29819028 ,29 ,57eb63d813fd7c0329bdb01f ,
2938 ,58462bc9a559fa7d29819028 ,30 ,57eb63d713fd7c0329bdafb5 ,57eb63d713fd7c0329bdafb6
And I also have
a dictionary named mydic
from "forDic.csv" for example:
{ '58462bc9a559fa7d29819028':'negative chin up', '57eb63d813fd7c0329bdb01f':'knee squeeze squat', '57eb63d713fd7c0329bdafb5: 'squat', '57eb63d713fd7c0329bdafb6':'lunge', ... }
I want to change values of "challenges.csv"
with values of mydic
if values of "challenges.csv"
is equal to keys of mydic
.
How can i do? Please help me.
Expected output: a csv file which contains rows like below
2937 ,'negative chin up' ,29 ,'knee squeeze squat' ,
2938 ,'negative chin up' ,30 ,'squat' ,'lunge'
import csv
with open('./forDic.csv', mode='r')as infile:
reader = csv.reader(infile)
mydic = dict((rows[0], rows[1]) for rows in reader)
print(mydic)
def replace_all()
with open('./challenges.csv', mode='r')as infile, open('./challenges_new.csv', mode='w') as outfile:
r = csv.reader(infile)
w = csv.writer(outfile)
for row in r:
for k in iter(mydic.keys()):
print(', '.join(row))
rl = [w.replace(str(k), str(mydic.values())) for w in rl]
print(rl[0])
row_list_string = ' / '.join(map(str, rl))
for k in list(mydic.keys()):
k = k.replace(k, mydic.get(k))
print(k)
replace_all()