2017-10-16 9 views
2

私はPythonで動的プログラム(タグに基づいてjsonファイルを解析する)を作成しようとしていますが、Pythonのexec関数を使用しています。しかし、exec文が関数内にある場合、プログラムは失敗しています。関数内のexec関数が機能していません(Python 3.5)

RUN 1:関数内 Execの:

import json 
from pandas.io.json import json_normalize 
import sys 
def param(dfile): 
    aString = "['widget']['image']~['widget']['window']~['widget']['text']" 
    for nd_part in aString.split('~'): 
     exec("temp = %s%s"%(dfile,nd_part)) 
     print(temp) 
if __name__ == "__main__": 
    dfile = json.load(open("sample_json.json")) 
    str_list = param(dfile) 

JSONデータ:sample_json.json

{"widget": { 
    "debug": "on", 
    "window": { 
     "title": "Sample Konfabulator Widget", 
     "name": "main_window", 
     "width": 500, 
     "height": 500 
    }, 
    "image": { 
     "src": "Images/Sun.png", 
     "name": "sun1", 
     "hOffset": 250, 
     "vOffset": 250, 
     "alignment": "center" 
    }, 
    "text": { 
     "data": "Click Here", 
     "size": 36, 
     "style": "bold", 
     "name": "text1", 
     "hOffset": 250, 
     "vOffset": 100, 
     "alignment": "center", 
     "onMouseUp": "sun1.opacity = (sun1.opacity/100) * 90;" 
    } 
}} 

エラー:

Traceback (most recent call last): 
    File "sample_test_json.py", line 12, in <module> 
    str_list = param(dfile) 
    File "sample_test_json.py", line 9, in param 
    print(temp) 
NameError: name 'temp' is not defined 

RUN 2:メインで EXEC:

import json 
from pandas.io.json import json_normalize 
import sys 
if __name__ == "__main__": 
    dfile = json.load(open("sample_json.json")) 
    aString = "['widget']['image']~['widget']['window']~['widget']['text']" 
    for nd_part in aString.split('~'): 
     exec("temp = %s%s"%(dfile,nd_part)) 
     print(temp) 

JSONデータ:sample_json.json(上記と同じデータ)

出力:エラーなし(予想通り結果)

{'hOffset': 250, 'vOffset': 250, 'name': 'sun1', 'alignment': 'center', 'src': 'Images/Sun.png'} 
{'width': 500, 'height': 500, 'name': 'main_window', 'title': 'Sample Konfabulator Widget'} 
{'data': 'Click Here', 'hOffset': 250, 'vOffset': 100, 'size': 36, 'style': 'bold', 'onMouseUp': 'sun1.opacity = (sun1.opacity/100) * 90;', 'name': 'text1', 'alignment': 'center'} 

RUN 3: 私はevalをしようとしたから、文字列の書式を設定しようとしましたこの郵便受け。 How to return value from exec in function?

import json 
from pandas.io.json import json_normalize 
import sys 
def param(dfile): 
    aString = "['widget']['image']~['widget']['window']~['widget']['text']" 
    for nd_part in aString.split('~'): 
     exec('temp = "{}""{}"'.format(dfile,nd_part)) 
     print(temp) 
if __name__ == "__main__": 
    dfile = json.load(open("sample_json.json")) 
    str_list = param(dfile) 

エラー:

Traceback (most recent call last): 
    File "sample_test_json.py", line 12, in <module> 
    str_list = param(dfile) 
    File "sample_test_json.py", line 9, in param 
    print(temp) 
NameError: name 'temp' is not defined 

問題を識別するのに私を助けてください。前もって感謝します。

+2

あなたはおそらく 'evalの()'を使用して、より良い運を持っていますが、実際には、そのための必要はありませんどちらか。 – kindall

+0

あなたは変数を設定しようとしています。 'exec()'の必要はありません。 – Cfreak

+0

はい。しかし、私の本来の要件は、各タグからデータフレームを作成し、区切り文字ファイルに書き込むことです。私のプログラムは、複数のタグを扱い、タグの数に基づいて複数のファイルを作成するためにダイナミックでなければなりません(これらのタグをパラメタとしてこのプログラムに渡すことを計画しています)。 – goks

答えて

2

Eval()を使用して、私はexepcted結果を得ています。以下の回答を投稿してください。しかし、まだ私はexec()がうまくいかない理由がわかりません。

import json 
from pandas.io.json import json_normalize 
import sys 
def param(dfile): 
    aString = "['widget']['image']~['widget']['window']~['widget']['text']" 
    for nd_part in aString.split('~'): 
     s = '{0}{1}'.format('dfile',nd_part) 
     temp = eval(s) 
     print(temp) 
if __name__ == "__main__": 
    dfile = json.load(open("sample_json.json")) 
    str_list = param(dfile) 

結果:

{'src': 'Images/Sun.png', 'alignment': 'center', 'vOffset': 250, 'name': 'sun1', 'hOffset': 250} 
{'title': 'Sample Konfabulator Widget', 'height': 500, 'width': 500, 'name': 'main_window'} 
{'alignment': 'center', 'onMouseUp': 'sun1.opacity = (sun1.opacity/100) * 90;', 'data': 'Click Here', 'hOffset': 250, 'size': 36, 'vOffset': 100, 'name': 'text1', 'style': 'bold'} 
+0

この問題が発生しました。ニースを見つける。ありがとう –

関連する問題