2017-06-10 14 views
0

JSONコレクションの数が(20〜40まで)異なる500個のフォルダがあります。私はしかし、それはこれが500回行わ必見です検討し、非常に骨の折れると明らかに非常に面倒になりhereherePython複数のフォルダからJSONコレクションを別々のタブ区切りファイルにエクスポートします。

import os, json 
path_to_json = 'C:/Users/SomeFolder' 
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')] 
#list all the files in the folder 
print (json_files) 

for js in json_files: 
    with open(os.path.join(path_to_json, js)) as json_file: 
#can only print to screen all the json files - need help saving to a tab-delimited file 
     print (json.load(json_file)) 

を与えられた答えを以下によりPythonで手動で個別に各フォルダの内容を抽出することができますよ。各JSONフォルダの内容をタブ区切りファイルに繰り返し抽出するためのより高速化された自動化されたアプローチが、大歓迎です。ありがとう

+0

'glob'モジュールを使用して、すべてのファイルに一致する正しい式を見つけます。例えば' */*。json'です。 –

+1

'os.walk()'を使うと、フォルダを再帰的に歩くことができます。 – zwer

答えて

1
import os, json 


current_directory = os.path.dirname(os.path.realpath(__file__)) 

all_directories = [x[0] for x in os.walk(current_directory)] 

for directory in all_directories: 
    path_to_json = directory 
    json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')] 
    #list all the files in the folder 
    print (json_files) 

    for js in json_files: 
     with open(os.path.join(path_to_json, js)) as json_file: 
    #can only print to screen all the json files - need help saving to a tab-delimited file 
      print (json.load(json_file)) 

このスクリプトを実行するすべてのフォルダが見つかります。次に、フォルダを繰り返してジョブを実行します。これをフォルダを保存する場所にコピーする必要があります。

+0

コードを編集して、タブ区切りのファイルに出力を保存しようとしました。上記を参照 –

関連する問題