0
ValueError: No JSON object could be decoded
コード:
def get_children(node):
for child in node['children']:
yield child
for grandchild in get_children(child):
yield grandchild
cell_info= open('/Users/massess/Desktop/assignment/cellinfo.text', 'w')
dir_path = '/Users /massess /Desktop /assignment /json'
for filename in os.listdir(dir_path):
cell_info.write("%s" %(filename))
f = open(os.path.join(dir_path,filename), 'r')
data = json.load(f)
#prints the root node
cell_info.write("%s\t%s\t%s\t%s\t%s\n" (data["CellName"],data["Event"], data["Minutes (E)"], data["X (E)"], data["Y (E)"]))
# prints all children of the root node
for child in get_children(data):
cell_info.write("%s\t%s\t%s\t%s\t%s\n" %(child["CellName"], child["Event"], child["Minutes (E)"], child["X (E)"], child["Y (E)"]))
cell_info.close()
私は1つの特定のJSONファイルを開くには、このコードを使用する場合しかし、それは動作します!
f = open('path\\to\\.json', 'r')
data = json.load(f)
は別にまだ破壊されるインデントから事前
インデントを修正してください。 Pythonでは重要です。それで、なぜ廊下にスペースがありますか? –
投稿コードのインデントを修正できますか? 'os.listdir'は、サブディレクトリとファイルをリストします。 'os.path.isfile'ファイルを使ってそれらをフィルタリングしたいかもしれません。そのディレクトリにはjson以外のファイルも含まれていますか? jsonファイルは、.jsonで終わるようなパターンに従っていますか?もしそうなら、おそらく 'glob.glob(" yourpath/*。jsoin ")'が最適な選択です。いずれの場合でも、失敗したファイル名を表示して、そのファイル名を確認してください。 – tdelaney
@tdelaneyあなたが言ったことの両方を試してみたところ、同じエラーが返ってきました いずれかの解決策を試してみると同じエラーが表示され、インデントも固定しました –