回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>请原谅我丑陋的新密码,我正在学习。我从omdbapi中提取电影数据,但是当我把它移到CSV时,我得到了许多电影的UnicodeEncodeError。可能是因为演员的名字有口音。我想1.)确定哪些电影有问题,2.)跳过它们,和/或3.)最好纠正错误。我目前所做的只是在错误发生时传递整个过程。寻找一个简单的解决办法,因为我是新手。在</p>
<pre><code>import csv
import os
import json
import omdb
movie_list = ['A Good Year', 'A Room with a View', 'Anchorman', 'Amélie', 'Annie Hall', 'Before Sunrise']
data_list = []
textdoc = open('textdoc.txt','w')
for w in movie_list:
x = omdb.request(t=w, fullplot=True, tomatoes=True, r='json')
y = x.content
z = json.loads(y)
data_list.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>([z["Title"], z["Year"], z["Actors"], z["Awards"], z["Director"], z["Genre"], z["Metascore"], z["Plot"], z["Rated"], z["Runtime"], z["Writer"], z["imdbID"], z["imdbRating"], z["imdbVotes"], z["tomatoRating"], z["tomatoReviews"], z["tomatoFresh"], z["tomatoRotten"], z["tomatoConsensus"], z["tomatoUserMeter"], z["tomatoUserRating"], z["tomatoUserReviews"]])
try:
with open('Films.csv', 'w') as g:
a = csv.writer(g, delimiter=',')
a.writerow(["Title", "Year", "Actors", "Awards", "Director", "Genre", "Metascore", "Plot", "Rated", "Runtime", "Writer", "imdbID", "imdbRating", "imdbVotes", "tomatoRating", "tomatoReviews", "tomatoFresh", "tomatoRotten", "tomatoConsensus", "tomatoUserMeter", "tomatoUserRating", "tomatoUserReviews"])
a.writerows(data_list)
except UnicodeEncodeError:
print("fail")
</code></pre>