質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
機械語

機械語とは、プロセッサが直接解釈・実行できる状態の言語です。

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Q&A

1回答

6884閲覧

jsonファイルをcsvファイルに変換時ValueErrorが起きてしまう。

Kokku

総合スコア39

機械語

機械語とは、プロセッサが直接解釈・実行できる状態の言語です。

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

0グッド

0クリップ

投稿2021/03/30 18:19

0 {'face_token': '3d03eb39f7479435db8ce3eb171f47b6', 'face_rectangle': {'top': 64, 'left': 31, 'width': 214, 'height': 214}, 'landmark': {'contour_chin': {'x': 117, 'y': 273}, 'contour_left1': {'x': 33, 'y': 101}, 'contour_left2': {'x': 33, 'y': 124}, 'contour_left3': {'x': 35, 'y': 147}, 'contour_left4': {'x': 37, 'y': 169}, 'contour_left5': {'x': 42, 'y': 192}, 'contour_left6': {'x': 49, 'y': 213}, 'contour_left7': {'x': 61, 'y': 233}, 'contour_left8': {'x': 76, 'y': 250}, 'contour_left9': {'x': 93, 'y': 265}, 'contour_right1': {'x': 247, 'y': 107}, 'contour_right2': {'x': 246, 'y': 133}, 'contour_right3': {'x': 242, 'y': 159}, 'contour_right4': {'x': 238, 'y': 184}, 'contour_right5': {'x': 230, 'y': 209}, 'contour_right6': {'x': 215, 'y': 230}, 'contour_right7': {'x': 195, 'y': 247}, 'contour_right8': {'x': 171, 'y': 260}, 'contour_right9': {'x': 146, 'y': 271}, 'left_eye_bottom': {'x': 77, 'y': 107}, 'left_eye_center': {'x': 79, 'y': 101}, 'left_eye_left_corner': {'x': 57, 'y': 101}, 'left_eye_lower_left_quarter': {'x': 66, 'y': 104}, 'left_eye_lower_right_quarter': {'x': 89, 'y': 107}, 'left_eye_pupil': {'x': 79, 'y': 99}, 'left_eye_right_corner': {'x': 100, 'y': 107}, 'left_eye_top': {'x': 79, 'y': 92}, 'left_eye_upper_left_quarter': {'x': 67, 'y': 94}, 'left_eye_upper_right_quarter': {'x': 91, 'y': 97}, 'left_eyebrow_left_corner': {'x': 42, 'y': 80}, 'left_eyebrow_lower_left_quarter': {'x': 55, 'y': 76}, 'left_eyebrow_lower_middle': {'x': 69, 'y': 77}, 'left_eyebrow_lower_right_quarter': {'x': 83, 'y': 84}, 'left_eyebrow_right_corner': {'x': 99, 'y': 89}, 'left_eyebrow_upper_left_quarter': {'x': 54, 'y': 65}, 'left_eyebrow_upper_middle': {'x': 71, 'y': 65}, 'left_eyebrow_upper_right_quarter': {'x': 87, 'y': 74}, 'mouth_left_corner': {'x': 78, 'y': 189}, 'mouth_lower_lip_bottom': {'x': 124, 'y': 230}, 'mouth_lower_lip_left_contour1': {'x': 98, 'y': 208}, 'mouth_lower_lip_left_contour2': {'x': 87, 'y': 208}, 'mouth_lower_lip_left_contour3': {'x': 101, 'y': 223}, 'mouth_lower_lip_right_contour1': {'x': 149, 'y': 210}, 'mouth_lower_lip_right_contour2': {'x': 159, 'y': 211}, 'mouth_lower_lip_right_contour3': {'x': 145, 'y': 224}, 'mouth_lower_lip_top': {'x': 124, 'y': 217}, 'mouth_right_corner': {'x': 168, 'y': 193}, 'mouth_upper_lip_bottom': {'x': 125, 'y': 198}, 'mouth_upper_lip_left_contour1': {'x': 117, 'y': 191}, 'mouth_upper_lip_left_contour2': {'x': 97, 'y': 189}, 'mouth_upper_lip_left_contour3': {'x': 102, 'y': 194}, 'mouth_upper_lip_right_contour1': {'x': 133, 'y': 191}, 'mouth_upper_lip_right_contour2': {'x': 151, 'y': 191}, 'mouth_upper_lip_right_contour3': {'x': 147, 'y': 195}, 'mouth_upper_lip_top': {'x': 125, 'y': 193}, 'nose_contour_left1': {'x': 109, 'y': 104}, 'nose_contour_left2': {'x': 100, 'y': 141}, 'nose_contour_left3': {'x': 105, 'y': 170}, 'nose_contour_lower_middle': {'x': 123, 'y': 178}, 'nose_contour_right1': {'x': 140, 'y': 105}, 'nose_contour_right2': {'x': 148, 'y': 144}, 'nose_contour_right3': {'x': 141, 'y': 172}, 'nose_left': {'x': 91, 'y': 158}, 'nose_right': {'x': 155, 'y': 162}, 'nose_tip': {'x': 124, 'y': 164}, 'right_eye_bottom': {'x': 173, 'y': 109}, 'right_eye_center': {'x': 172, 'y': 104}, 'right_eye_left_corner': {'x': 152, 'y': 106}, 'right_eye_lower_left_quarter': {'x': 162, 'y': 108}, 'right_eye_lower_right_quarter': {'x': 184, 'y': 108}, 'right_eye_pupil': {'x': 172, 'y': 102}, 'right_eye_right_corner': {'x': 193, 'y': 105}, 'right_eye_top': {'x': 172, 'y': 95}, 'right_eye_upper_left_quarter': {'x': 161, 'y': 98}, 'right_eye_upper_right_quarter': {'x': 183, 'y': 98}, 'right_eyebrow_left_corner': {'x': 150, 'y': 92}, 'right_eyebrow_lower_left_quarter': {'x': 169, 'y': 90}, 'right_eyebrow_lower_middle': {'x': 186, 'y': 86}, 'right_eyebrow_lower_right_quarter': {'x': 202, 'y': 84}, 'right_eyebrow_right_corner': {'x': 216, 'y': 87}, 'right_eyebrow_upper_left_quarter': {'x': 166, 'y': 79}, 'right_eyebrow_upper_middle': {'x': 184, 'y': 72}, 'right_eyebrow_upper_right_quarter': {'x': 204, 'y': 72}}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 26}, 'emotion': {'anger': 98.934, 'disgust': 1.06, 'fear': 0.002, 'happiness': 0.001, 'neutral': 0.001, 'sadness': 0.001, 'surprise': 0.002}}} 1 {'face_token': '3d03eb39f7479435db8ce3eb171f47b6', 'face_rectangle': {'top': 64, 'left': 31, 'width': 214, 'height': 214}, 'landmark': {'contour_chin': {'x': 117, 'y': 273}, 'contour_left1': {'x': 33, 'y': 101}, 'contour_left2': {'x': 33, 'y': 124}, 'contour_left3': {'x': 35, 'y': 147}, 'contour_left4': {'x': 37, 'y': 169}, 'contour_left5': {'x': 42, 'y': 192}, 'contour_left6': {'x': 49, 'y': 213}, 'contour_left7': {'x': 61, 'y': 233}, 'contour_left8': {'x': 76, 'y': 250}, 'contour_left9': {'x': 93, 'y': 265}, 'contour_right1': {'x': 247, 'y': 107}, 'contour_right2': {'x': 246, 'y': 133}, 'contour_right3': {'x': 242, 'y': 159}, 'contour_right4': {'x': 238, 'y': 184}, 'contour_right5': {'x': 230, 'y': 209}, 'contour_right6': {'x': 215, 'y': 230}, 'contour_right7': {'x': 195, 'y': 247}, 'contour_right8': {'x': 171, 'y': 260}, 'contour_right9': {'x': 146, 'y': 271}, 'left_eye_bottom': {'x': 77, 'y': 107}, 'left_eye_center': {'x': 79, 'y': 101}, 'left_eye_left_corner': {'x': 57, 'y': 101}, 'left_eye_lower_left_quarter': {'x': 66, 'y': 104}, 'left_eye_lower_right_quarter': {'x': 89, 'y': 107}, 'left_eye_pupil': {'x': 79, 'y': 99}, 'left_eye_right_corner': {'x': 100, 'y': 107}, 'left_eye_top': {'x': 79, 'y': 92}, 'left_eye_upper_left_quarter': {'x': 67, 'y': 94}, 'left_eye_upper_right_quarter': {'x': 91, 'y': 97}, 'left_eyebrow_left_corner': {'x': 42, 'y': 80}, 'left_eyebrow_lower_left_quarter': {'x': 55, 'y': 76}, 'left_eyebrow_lower_middle': {'x': 69, 'y': 77}, 'left_eyebrow_lower_right_quarter': {'x': 83, 'y': 84}, 'left_eyebrow_right_corner': {'x': 99, 'y': 89}, 'left_eyebrow_upper_left_quarter': {'x': 54, 'y': 65}, 'left_eyebrow_upper_middle': {'x': 71, 'y': 65}, 'left_eyebrow_upper_right_quarter': {'x': 87, 'y': 74}, 'mouth_left_corner': {'x': 78, 'y': 189}, 'mouth_lower_lip_bottom': {'x': 124, 'y': 230}, 'mouth_lower_lip_left_contour1': {'x': 98, 'y': 208}, 'mouth_lower_lip_left_contour2': {'x': 87, 'y': 208}, 'mouth_lower_lip_left_contour3': {'x': 101, 'y': 223}, 'mouth_lower_lip_right_contour1': {'x': 149, 'y': 210}, 'mouth_lower_lip_right_contour2': {'x': 159, 'y': 211}, 'mouth_lower_lip_right_contour3': {'x': 145, 'y': 224}, 'mouth_lower_lip_top': {'x': 124, 'y': 217}, 'mouth_right_corner': {'x': 168, 'y': 193}, 'mouth_upper_lip_bottom': {'x': 125, 'y': 198}, 'mouth_upper_lip_left_contour1': {'x': 117, 'y': 191}, 'mouth_upper_lip_left_contour2': {'x': 97, 'y': 189}, 'mouth_upper_lip_left_contour3': {'x': 102, 'y': 194}, 'mouth_upper_lip_right_contour1': {'x': 133, 'y': 191}, 'mouth_upper_lip_right_contour2': {'x': 151, 'y': 191}, 'mouth_upper_lip_right_contour3': {'x': 147, 'y': 195}, 'mouth_upper_lip_top': {'x': 125, 'y': 193}, 'nose_contour_left1': {'x': 109, 'y': 104}, 'nose_contour_left2': {'x': 100, 'y': 141}, 'nose_contour_left3': {'x': 105, 'y': 170}, 'nose_contour_lower_middle': {'x': 123, 'y': 178}, 'nose_contour_right1': {'x': 140, 'y': 105}, 'nose_contour_right2': {'x': 148, 'y': 144}, 'nose_contour_right3': {'x': 141, 'y': 172}, 'nose_left': {'x': 91, 'y': 158}, 'nose_right': {'x': 155, 'y': 162}, 'nose_tip': {'x': 124, 'y': 164}, 'right_eye_bottom': {'x': 173, 'y': 109}, 'right_eye_center': {'x': 172, 'y': 104}, 'right_eye_left_corner': {'x': 152, 'y': 106}, 'right_eye_lower_left_quarter': {'x': 162, 'y': 108}, 'right_eye_lower_right_quarter': {'x': 184, 'y': 108}, 'right_eye_pupil': {'x': 172, 'y': 102}, 'right_eye_right_corner': {'x': 193, 'y': 105}, 'right_eye_top': {'x': 172, 'y': 95}, 'right_eye_upper_left_quarter': {'x': 161, 'y': 98}, 'right_eye_upper_right_quarter': {'x': 183, 'y': 98}, 'right_eyebrow_left_corner': {'x': 150, 'y': 92}, 'right_eyebrow_lower_left_quarter': {'x': 169, 'y': 90}, 'right_eyebrow_lower_middle': {'x': 186, 'y': 86}, 'right_eyebrow_lower_right_quarter': {'x': 202, 'y': 84}, 'right_eyebrow_right_corner': {'x': 216, 'y': 87}, 'right_eyebrow_upper_left_quarter': {'x': 166, 'y': 79}, 'right_eyebrow_upper_middle': {'x': 184, 'y': 72}, 'right_eyebrow_upper_right_quarter': {'x': 204, 'y': 72}}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 26}, 'emotion': {'anger': 98.934, 'disgust': 1.06, 'fear': 0.002, 'happiness': 0.001, 'neutral': 0.001, 'sadness': 0.001, 'surprise': 0.002}}} . . . .

このような形式のjsonファイルを機械学習に使うためcsvファイル形式に直したいのですがエラーが出てしまいます。

ValueError: Trailing data

調べるとlines=Trueを入れるといいとかいてあったので入れてみるとまた違うエラーが出てきました。

ValueError: Unexpected character found when decoding array value (2)

aaa.py

1# coding:utf-8 2 3#Pandasをインポート 4import pandas as pd 5import json 6from pandas.io.json import json_normalize 7 8#変換したいJSONファイルを読み込む 9df = pd.read_json('emotion.json',lines=True) 10#print(df) 11 12# read_jsonした結果だとネストしたjsonを展開できないのでnormalizeで展開させる 13df_json = json_normalize(df['data']) 14 15#ネストしていないシンプルなJSONであれば、json_normalizeも不要なはず。 16#ネストが含まれている場合は、上記で解決できると思います。 17df.to_csv("emotion3.csv", encoding='utf-8')

どこに原因があるのかがわからず質問させていただきました。
何か解決方法があれば教えていただきたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

このような形式のjsonファイル

記載しているデータはjsonではないためエラーとなります。

解決案

取り急ぎ読み込むには先頭にある0,1を削除し、以下の形式にします。
(ちなみに、シングルクォートもjsonとしては許されていないのですが、pandasはシングルクォートでも読み込めるようです)

json

1[ 2{"face_token":.....}, 3{"face_token":.....} 4]

そして、読み込みを以下のようにすると読み込むことができました。
質問の本題ではないため読み込み以降のコードは試していません( data というカラムはないのでエラーになるのではないかと思います。)

python

1df = pd.read_json('emotion.json')

投稿2021/03/31 01:50

編集2021/03/31 01:53
yamap55

総合スコア1376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kokku

2021/03/31 14:12

シングルクォーテーションで作られたこのファイルをpythonでダブルクォーテーションに変換することは可能でしょうか?
yamap55

2021/04/01 04:53

可能です。 方法まで聞きたいのであれば、本質問からはズレるので別質問の方が良いでしょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問