2017-03-08 16 views
0

私はPythonの新機能です。私は過去にいくつかの初心者コースを受講しましたが、現時点では、仕事中の特定の仕事を支援するためのスクリプトをいくつか作成しました(主にデータの収集/解析に関連しています)。私が今しようとしているのは、ユーザーIDのリスト(配列に入れられたraw_inputから取得したもの)を取得し、JSON形式でそれらの数値のリストを出力することです。結果は以下のようなものを見る必要があります:配列の各項目をJSON形式で表示する

{ 
    "user_id": "12345", 
    "name": "Bob", 
    "comment": "" 
} 

これは私がこれまで持っているものです。

script_users = map(int, raw_input("Please enter your user IDs, seperated by space with no comma: ").split()) 
final_users = list(set(script_users)) 

format = """ "name": "Bob", "comment": "" """ 

アイデアがあることを利用して、各ユーザーIDのリストをプリントアウトする私の形式の変数を使用していました特定のフォーマット。私はこれを行うためにループを使用する必要があることを知っていますが、私はそれらにあまり慣れていません。誰も助けることができますか?ありがとう。 JSONをエンコードするために

+0

PythonデータをJSON形式で印刷するには、 'json'モジュールを使用します。 – Barmar

+1

https://docs.python.org/2/library/json.htmlを参照してください。 – Barmar

答えて

0

、あなたはJSONモジュールをインポートして、ダンプ・メソッドを使用して、例えば、負荷の方法を使用することができますJSONをデコードすることができます。

import json 


json_data_encoding = json.dumps(
     {'user_id': 1, 'name': 'bob', 'comment': 'this is a comment'} 
) 

print(json_data_encoding) 
# output: {"name": "bob", "user_id": 1, "comment": "this is a comment"} 

json_data_decoding = json.loads(json_data_encoding) 
print(json_data_decoding['name']) 
# output: bob 

従うリストを生成するために対処するためには、下のコードでは、リストをループする方法も示しています。

list_json_data_encoding = json.dumps([ 
    {'user_id': 1, 'name': 'bob', 'comment': 'this is a comment'}, 
    {'user_id': 2, 'name': 'tom', 'comment': 'this is another comment'} 
] 
) 

print(list_json_data_encoding) 
# output: [{"name": "bob", "user_id": 1, "comment": "this is a comment"}, {"name": "tom", "user_id": 2, "comment": "this is another comment"}] 

list_json_data_decoding = json.loads(list_json_data_encoding) 

for json_data_decoded in list_json_data_decoding: 
    print(json_data_decoded['name']) 
# output1: bob 
# output2: tom 

希望します。

これはあなたが探しているものですか?

import json 


script_users = map(str, input("Please enter your user names, seperated by space with no comma: ").split()) 
users = list(set(script_users)) 

list_users = [] 
for user in users: 
    list_users.append({'user_id': 1, 'name': user, 'comment': 'this is a comment'}) 

json_user_list_encoding = json.dumps(list_users) 

print(json_user_list_encoding) 
# this will return a json list where user_id will always be 1 and comment will be 'this is a comment', 
# the name will change for every user inputed by a space 

json_user_list_decoding = json.loads(json_user_list_encoding) 

for json_user_decoded in json_user_list_decoding: 
    print(json_user_decoded['name']) 
# output: return the names as the where entered originally 
+0

フォローアップとして、ユーザーがraw_inputを使用して名前を入力するように求められ、その名前が配列に格納されているとします。これらの名前を1つ1つループしてJSONオブジェクトにそれぞれ出力するループを作成する方法はありますか?たとえば、ユーザーが「Steve、John、Ted」と入力したとします。ループがこれらの名前を通過して{"name": "Steve"、 "user_id":1、 "comment": "これはコメントです"}、{"name": "John" "user_id":1、 "comment": "これはコメントです"}、{"name": "Ted"、 "user_id":1、 "comment": "これはコメントです}"これは既に表示されていれば申し訳ありません – user7681184

+0

これはあなたが@ user7681184を達成するために探しているものです、私はraw_inputがpython 3で使用されなくなったので、私はpython 3を使用しています。 – Chris

+0

Chrisありがとうございます。 – user7681184

関連する問題