JSONファイルをデータフレームに収めようとしています。私の現在の盗聴コードは、以下の方法でJSONファイルを作成します。JSONファイルをpandasデータフレームにフィッティングする際のエラー
fname = 'python.json'
with open(fname, 'r') as f, open('sentiment.json', 'w') as s:
for line in f:
tweet = json.loads(line)
# Create a list with all the terms
tweet_words = tweet['text']
output = subprocess.check_output(['curl', '-d', "text=" + tweet_words.encode('utf-8'), 'http://text-processing.com/api/sentiment/'])
s.write(output+"\n")
それはtext-processing.comのAPIから要求された「sentiment.json」出力に書き込みます。私は、使用してJSONを読み込む:
def load_json(file, skip):
with open(file, 'r') as f:
read = f.readlines()
json_data = (json.loads(line) for i, line in enumerate(read) if i%skip==0)
return json_data
をそして使用してデータフレームを構築:
sentiment_df = load_json('sentiments.json', 1)
data = {'positive': [], 'negative': [], 'neutral': []}
for s in sentiment_df:
data['positive'].append(s['probability']['pos'])
data['negative'].append(s['probability']['neg'])
data['neutral'].append(s['probability']['neutral'])
df = pd.DataFrame(data)
エラー:とValueError:いいえJSONオブジェクトは
をデコードすることができ、私はいくつかの関連の質問を通じて閲覧しましたWoodrowShigeruの答えhereに基づいて、私はコードの最初のブロックで 'utf-8'へのエンコーディングと関係があると思われます。
誰でも良い修正を知っていますか?または、少なくともいくつかの指示を提供しますか?みんなありがとう!コンテナはすべてカンマで区切られた行の項目を保持しなければならないよう
編集1