我遇到从json转换为csv文件的问题。我有这些数据:
x = ''' [
{u'Plot': u'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', u'Rated': u'R', u'Title': u'The Shawshank Redemption', u'Ratings': [{u'Source': u'Internet Movie Database', u'Value': u'9.3/10'}, {u'Source': u'Rotten Tomatoes', u'Value': u'91%'}, {u'Source': u'Metacritic', u'Value': u'80/100'}], u'DVD': u'N/A', u'Writer': u'Stephen King (short story "Rita Hayworth and Shawshank Redemption"), Frank Darabont (screenplay)', u'Production': u'Columbia Pictures', u'Actors': u'Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler', u'Type': u'movie', u'imdbVotes': u'1,814,175', u'Website': u'N/A', u'Director': u'Frank Darabont', u'Released': u'14 Oct 1994', u'Awards': u'Nominated for 7 Oscars. Another 19 wins & 30 nominations.', u'Genre': u'Crime, Drama', u'imdbRating': u'9.3', u'Language': u'English', u'Country': u'USA', u'BoxOffice': u'N/A', u'Runtime': u'142 min', u'imdbID': u'tt0111161', u'Metascore': u'80', u'Response': u'True', u'Year': u'1994'}
{u'Plot': u'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', u'Rated': u'R', u'Title': u'The Godfather', u'Ratings': [{u'Source': u'Internet Movie Database', u'Value': u'9.2/10'}, {u'Source': u'Rotten Tomatoes', u'Value': u'99%'}, {u'Source': u'Metacritic', u'Value': u'100/100'}], u'DVD': u'09 Oct 2001', u'Writer': u'Mario Puzo (screenplay), Francis Ford Coppola (screenplay), Mario Puzo (novel)', u'Production': u'Paramount Pictures', u'Actors': u'Marlon Brando, Al Pacino, James Caan, Richard S. Castellano', u'Type': u'movie', u'imdbVotes': u'1,235,597', u'Website': u'http://www.thegodfather.com', u'Director': u'Francis Ford Coppola', u'Released': u'24 Mar 1972', u'Awards': u'Won 3 Oscars. Another 23 wins & 27 nominations.', u'Genre': u'Crime, Drama', u'imdbRating': u'9.2', u'Language': u'English, Italian, Latin', u'Country': u'USA', u'BoxOffice': u'N/A', u'Runtime': u'175 min', u'imdbID': u'tt0068646', u'Metascore': u'100', u'Response': u'True', u'Year': u'1972'}
]'''
我正在尝试将此转换为csv,以便能够从所有细节中获取标题和年份。当我尝试执行此操作时:json.loads(x)
,出现错误:"ValueError: Expecting property name: line 2 column 2 (char 3)"
,我不知道如何修复它?有什么想法吗?
答案 0 :(得分:1)
您的数据不是有效的JSON格式。然而,它几乎是 有效的Python。以下是如何利用这一事实并做你想做的事情:
import ast
import csv
x = ''' [
{u'Plot': u'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', u'Rated': u'R', u'Title': u'The Shawshank Redemption', u'Ratings': [{u'Source': u'Internet Movie Database', u'Value': u'9.3/10'}, {u'Source': u'Rotten Tomatoes', u'Value': u'91%'}, {u'Source': u'Metacritic', u'Value': u'80/100'}], u'DVD': u'N/A', u'Writer': u'Stephen King (short story "Rita Hayworth and Shawshank Redemption"), Frank Darabont (screenplay)', u'Production': u'Columbia Pictures', u'Actors': u'Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler', u'Type': u'movie', u'imdbVotes': u'1,814,175', u'Website': u'N/A', u'Director': u'Frank Darabont', u'Released': u'14 Oct 1994', u'Awards': u'Nominated for 7 Oscars. Another 19 wins & 30 nominations.', u'Genre': u'Crime, Drama', u'imdbRating': u'9.3', u'Language': u'English', u'Country': u'USA', u'BoxOffice': u'N/A', u'Runtime': u'142 min', u'imdbID': u'tt0111161', u'Metascore': u'80', u'Response': u'True', u'Year': u'1994'}
{u'Plot': u'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', u'Rated': u'R', u'Title': u'The Godfather', u'Ratings': [{u'Source': u'Internet Movie Database', u'Value': u'9.2/10'}, {u'Source': u'Rotten Tomatoes', u'Value': u'99%'}, {u'Source': u'Metacritic', u'Value': u'100/100'}], u'DVD': u'09 Oct 2001', u'Writer': u'Mario Puzo (screenplay), Francis Ford Coppola (screenplay), Mario Puzo (novel)', u'Production': u'Paramount Pictures', u'Actors': u'Marlon Brando, Al Pacino, James Caan, Richard S. Castellano', u'Type': u'movie', u'imdbVotes': u'1,235,597', u'Website': u'http://www.thegodfather.com', u'Director': u'Francis Ford Coppola', u'Released': u'24 Mar 1972', u'Awards': u'Won 3 Oscars. Another 23 wins & 27 nominations.', u'Genre': u'Crime, Drama', u'imdbRating': u'9.2', u'Language': u'English, Italian, Latin', u'Country': u'USA', u'BoxOffice': u'N/A', u'Runtime': u'175 min', u'imdbID': u'tt0068646', u'Metascore': u'100', u'Response': u'True', u'Year': u'1972'}
]'''
data = [ast.literal_eval(s) for s in x.split('\n') if s.startswith('{')]
with open('movie_data.csv', 'w', newline='') as file:
fieldnames = list(data[0].keys())
writer = csv.DictWriter(file, fieldnames, quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader() # optional
for obj in data:
writer.writerow(obj)
创建movie_data.csv
的内容:
输出:
"Plot","Rated","Title","Ratings","DVD","Writer","Production","Actors","Type","imdbVotes","Website","Director","Released","Awards","Genre","imdbRating","Language","Country","BoxOffice","Runtime","imdbID","Metascore","Response","Year"
"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.","R","The Shawshank Redemption","[{'Source': 'Internet Movie Database', 'Value': '9.3/10'}, {'Source': 'Rotten Tomatoes', 'Value': '91%'}, {'Source': 'Metacritic', 'Value': '80/100'}]","N/A","Stephen King (short story ""Rita Hayworth and Shawshank Redemption""), Frank Darabont (screenplay)","Columbia Pictures","Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler","movie","1,814,175","N/A","Frank Darabont","14 Oct 1994","Nominated for 7 Oscars. Another 19 wins & 30 nominations.","Crime, Drama","9.3","English","USA","N/A","142 min","tt0111161","80","True","1994"
"The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.","R","The Godfather","[{'Source': 'Internet Movie Database', 'Value': '9.2/10'}, {'Source': 'Rotten Tomatoes', 'Value': '99%'}, {'Source': 'Metacritic', 'Value': '100/100'}]","09 Oct 2001","Mario Puzo (screenplay), Francis Ford Coppola (screenplay), Mario Puzo (novel)","Paramount Pictures","Marlon Brando, Al Pacino, James Caan, Richard S. Castellano","movie","1,235,597","http://www.thegodfather.com","Francis Ford Coppola","24 Mar 1972","Won 3 Oscars. Another 23 wins & 27 nominations.","Crime, Drama","9.2","English, Italian, Latin","USA","N/A","175 min","tt0068646","100","True","1972"