I have a csv file and content is as below:
config,"[{""enabled"" : false,""id"" : ""name_of_app""}]"
region, US
app_name, test
I want to open csv, update "enabled" = false
to "enabled" = true
. I could do that if that's not a csv.
What I have done so far:
for name in filelist:
vfile = open(name, "r")
with open(name) as vfile:
reader = csv.reader(vfile) # Create a new reader
for row in reader:
payload = row[1]
if row[0] == "config":
print("hello")
json_payload = row[1]
json_payload=json.loads(json_payload)
CodePudding user response:
You almost reach the solution of the problem.
import csv
import json
filelist = ['*.csv', '*.csv']
for name in filelist:
rows = []
flag = False
with open(name) as vfile:
reader = csv.reader(vfile) # Create a new reader
for row in reader:
if row[0] == "config":
flag = True
json_payload = json.loads(row[1])
json_payload[0]["enabled"] = True
row[1] = json.dumps(json_payload)
rows.append(row)
if flag:
with open(name, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(rows)