2017-06-05 32 views
1

我有從json轉換爲csv文件的問題。我有這樣的數據:將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

爲什麼要添加'u'特里普爾報價裏面? 刪除或使用csv模塊直接傳遞數據到json模塊而不是原始字符串 –

+1

我覺得這個問題是由於'u'前綴是有效的Python語法,但沒有有效的json語法 – turbulencetoo

+0

的東西是它被生成從循環,我爲了得到這些細節,自動,我真的不知道如何擺脫這一點。這裏是我使用的一個循環:對於我在ids中的: url ='http://www.omdbapi.com/?i={}'.format(i)+apikey r = requests.get(url) val = json.loads(r.text) print val – Doro

回答

1

您的數據不是有效的JSON格式。這是差不多有效的Python,但是。以下是如何利用這一事實,並做你想做什麼:創建movie_data.csv

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) 

內容:

輸出:

"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" 
+0

謝謝你,但是我在python中是新的,我真的不明白+新的錯誤出現。有沒有更簡單的方法來做我所需要的?謝謝 – Doro

+0

Doro:對不起,這只是我能做到的簡單。我建議你花點時間瞭解它在做什麼 - 因爲在我看來這是非常值得的(並且可能允許你解決新問題)。如果事後不能,那麼隨時發表一個描述他們的新問題。 – martineau