Skip to content

Instantly share code, notes, and snippets.

@dehio3
Last active April 24, 2019 05:26
Show Gist options
  • Save dehio3/0bb85c76918f719fbf3fb983c34eb8a9 to your computer and use it in GitHub Desktop.
Save dehio3/0bb85c76918f719fbf3fb983c34eb8a9 to your computer and use it in GitHub Desktop.
ローカルにあるdict形式のデータファイルをjson形式のデータファイルに変換
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ast
import glob
import json
import os
def dict_to_json(filename):
new_file_name = '{}.new'.format(filename)
if os.path.isfile(new_file_name):
os.remove(new_file_name)
with open(filename) as f:
for line in f:
# str to dict
line_dict = ast.literal_eval(line)
# dict to json
line_json = json.dumps(line_dict)
# add new file
with open(new_file_name, 'a') as new_f:
new_f.write('{}\n'.format(line_json))
os.rename(new_file_name, filename)
def main():
#file_list = os.listdir('.')
file_list = glob.glob('./*.json')
for file in file_list:
print('{} start'.format(file))
dict_to_json(file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment