我有從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)"
,我不知道如何解決它?有任何想法嗎?
爲什麼要添加'u'特里普爾報價裏面? 刪除或使用csv模塊直接傳遞數據到json模塊而不是原始字符串 –
我覺得這個問題是由於'u'前綴是有效的Python語法,但沒有有效的json語法 – turbulencetoo
的東西是它被生成從循環,我爲了得到這些細節,自動,我真的不知道如何擺脫這一點。這裏是我使用的一個循環:對於我在ids中的: url ='http://www.omdbapi.com/?i={}'.format(i)+apikey r = requests.get(url) val = json.loads(r.text) print val – Doro