2017-12-29 8 views
0

나는 GitHub에 here 발견 csv 구문 분석 json 조정하려고합니다. 내가 다른 파이썬에서 실행하기 위해 호출 할 수 있도록 내가 수정 한 코드에 노력하고모듈 문제 args 함께 전달

을 만들 노드, JSON 파일에 경로, CSV로 경로 : 코드는 정의 3 개 인수 터미널에서 실행되도록 설정되어 내가 쓰고있는 스크립트. 터미널에서 실행되는 모듈에 대해 배운 내용에서 if __name__ == "__main__":을 사용하지만 다른 파이썬 스크립트에서 실행하려면 def main()과 같은 정의를 만들어야합니다.

Traceback (most recent call last): 
    File "/Users/Documents/Projects/test.py", line 8, in <module> 
    jsontocsv2.main('cars', SourceFile, filename) 
    File "/Users/Documents/Projects/jsontocsv2.py", line 84, in main 
    reduce_item(node, item) 
    File "/Users/Documents/Projects/jsontocsv2.py", line 57, in reduce_item 
    reduce_item(key + '_' + to_string(sub_key), value[sub_key]) 
    File "/Users/Documents/Projects/jsontocsv2.py", line 61, in reduce_item 
    reduced_item[to_string(key)] = to_string(value) 
NameError: name 'reduced_item' is not defined 

수있는 사람의 도움을 포인트 : 여기

import jsontocsv2 
import json 

filename = 'test2.csv' 

SourceFile = 'carapi.json' 

jsontocsv2.main('cars', SourceFile, filename) 

내가지고있어 오류입니다 : 여기
import sys 
import json 
import csv 

# https://github.com/vinay20045/json-to-csv 
## 
# Convert to string keeping encoding in mind... 
## 


def to_string(s): 
    try: 
     return str(s) 
    except: 
     # Change the encoding type if needed 
     return s.encode('utf-8') 

def reduce_item(key, value): 
    global reduced_item 

    # Reduction Condition 1 
    if type(value) is list: 
     i = 0 
     for sub_item in value: 
      reduce_item(key + '_' + to_string(i), sub_item) 
      i = i + 1 

    # Reduction Condition 2 
    elif type(value) is dict: 
     sub_keys = value.keys() 
     for sub_key in sub_keys: 
      reduce_item(key + '_' + to_string(sub_key), value[sub_key]) 

    # Base Condition 
    else: 
     reduced_item[to_string(key)] = to_string(value) 

# the module I created and moved the contents of __main__ to here 
def main(node, json_file_path, csv_file_path): 
    # Reading arguments 
    # node = sys.argv[1] 
    # json_file_path = sys.argv[2] 
    # csv_file_path = sys.argv[3] 

    fp = open(json_file_path, 'r') 
    json_value = fp.read() 
    raw_data = json.loads(json_value) 
    print(raw_data['tag']) 

    try: 
     data_to_be_processed = raw_data[node] 
    except: 
     data_to_be_processed = raw_data 

    processed_data = [] 
    header = [] 
    for item in data_to_be_processed: 
     reduced_item = {} 
     reduce_item(node, item) 

     header += reduced_item.keys() 

     processed_data.append(reduced_item) 

    header = list(set(header)) 
    header.sort() 

    with open(csv_file_path, 'a') as f: 
     writer = csv.DictWriter(f, header, quoting=csv.QUOTE_ALL) 
     writer.writeheader() 
     for row in processed_data: 
      writer.writerow(row) 

    print ("Just completed writing csv file with %d columns" % len(header)) 


# if __name__ == "__main__": 
#  if len(sys.argv) != 4: 
#   print ("\nUsage: python json_to_csv.py <node_name> <json_in_file_path> <csv_out_file_path>\n") 
#  else: 
#   # Reading arguments 
#  main(sys.argv) 

내가 jsontocsv2.py 전화를 사용하고있는 다른 파이썬 스크립트입니다 이 문제를 해결하는 방법에 대한 올바른 방향? 스택 오버플로에 대해 많은 검색을 수행하고 유사한 문제가있는 게시물을 발견했지만이를 작동시키는 방법을 파악할 수 없었습니다.

+4

'글로벌'로 정의하는 것은 일반적으로 좋은 아이디어는 아닙니다. 'reduced_item'을 없애고, 함수 정의를'def reduce_item (key, value, reduced_item) :'로 변경하고'reduced_item = reduce_item (node, item, reduced_item)'을 사용하여'main()'내부에서 호출하십시오. 'reduce_item (node, item)'중 하나입니다. 혼자서 사용할 수 있는지에 대해서는 확실치 않습니다. – roganjosh

+0

제안이 효과가 있습니까? 차라리 당신의 json에 대해 추측하지 않고 처음부터 테스트를 직접 해보려고합니다.하지만 내 제안이 당신에게 어떤 피드백을 주 었는지에 대한 피드백이 없습니다. – roganjosh

+1

아마도 Namerror 문제와 관련이 없겠지만 GitHub 페이지에 따르면 json_to_csv.py는 Python 2.7 용으로 작성되었습니다. 버전 3을 사용하는 경우 다른 문제가 발생할 수 있습니다. – martineau

답변

0

내가 원하는 방식으로 코드를 사용할 수있었습니다.

global reduced_item() 문을 def reduced_item() 기능에서 만든 def main(node, json_file_path, csv_file_path) 기능으로 옮기면됩니다. 글로벌 변수가 정의되지 않았다는 것을 말하면, 왜 이것이 작동하는지 잘 모르겠습니다.

또한 왜 무언가를 정의하는 것이 일반적으로 좋은 아이디어가 아닌지 Global입니까? 너희들이이 일을 더 잘 수행 할 수있는 방법에 대한 권고를 가지고 있다면 나는 지침을 위해 열어 둔다. 도와 주셔서 감사합니다.

+0

'global reduced_item' 문을 움직이는 이유가 도움이되었습니다. 왜냐하면'reduced_item'이'main()'함수의 로컬 변수 대신에 모듈 레벨의 "global"변수가되기 때문입니다. 그래서 다른 함수'reduce_item()'에서 그 이름을 참조 할 수있게되었습니다. (함수에서도'global'라고 선언했는지 여부에 상관없이) 이제는 reduce_item()을 올바르게 해석 할 수 있습니다. Python의 범위 지정 규칙이 어떻게 작동하는지에 대한 좋은 설명을 위해 Python에서 이름을 결정하는 방식 때문입니다.이 [답변] (https://stackoverflow.com/a/292502/355230)은 다른 질문에 대한 것입니다. – martineau