I am trying to update the actors list in a JSON file. I am using a for loop to grab the list of actors but when I try updating the list with an if clause, the output is unchanged. I want to change "Alan Arkin" to "John Doe"
The output that I am getting now is: "actors": ["Ethan Hawke", "Uma Thurman", "Alan Arkin", "Loren Dean"]
The desired out put would be: "actors": ["Ethan Hawke", "Uma Thurman", "John Doe", "Loren Dean"]
This is what is inside "movie_1.txt" file
{ "title": "Gattaca", "release_year": 1997, "is_awesome": true, "won_oscar": false, "actors": ["Ethan Hawke", "Uma Thurman", "Alan Arkin", "Loren Dean"], "budget": null, "credits": { "director": "Andrew Niccol", "writer": "Andrew Niccol", "composer": "Michael Nyman", "cinematographer": "Slawomir Idziak" } }
What my code looks like right now. My issues is the for loop with a nested if clause that grabs the name in the list. The for loop through the list and the if clause should grab an instance of "Alan Arkin" and change it to "John Doe"
path = 'movie_1.txt'
import json
json_file = open(path, encoding="utf-8")
movie = json.load(json_file)
json_file.close()
print(movie['actors']) # prints unchanged list
for x in movie["actors"]:
if x == 'Alan Arkin':
x = 'John Doe'
print(movie['actors']) # prints changed list