2017-06-05 8 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のi: 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

道:申し訳ありませんが、これは私がそれを作ることができるほど簡単です。私はそれが何をしているのか理解する時間をとることをお勧めします。それは私の意見では非常に価値があるでしょう(そしておそらくあなたは新しい問題が何であれ修正することができます)。その後はできない場合は、自由に新しい質問を投稿してください。 – martineau

関連する問題