Open RoyMoto opened 5 years ago
[
{
"DEFINE" : "I'm afrid of X",
"PREVENT" : "I can prevetn is so",
"REPAIR" : "I can fix it so"
},
{
"DEFINE" : "I'm afrid of X",
"PREVENT" : "I can prevetn is so",
"REPAIR" : "I can fix it so"
}
]
#!/usr/bin/env python3
import csv
import json
import argparse
import os
def main():
# set args
parser = argparse.ArgumentParser(description='convert json to csv.')
parser.add_argument('-i', '--input_json',
nargs=1,
type=str,
help='an input json file',
dest='input_json',
required=True)
parser.add_argument('-o', '--output_dir',
nargs=1,
type=str,
default=['.'],
help='output directory',
dest='output_dir',
required=False)
# get args
args = parser.parse_args()
# get json file name and output path
json_file = args.input_json[0]
output_dir = args.output_dir[0]
csv_file = os.path.basename(json_file)
csv_file = os.path.splitext(csv_file)
csv_file = csv_file[0] + '.csv'
csv_file = os.path.join(output_dir, csv_file)
# load json as dict
with open(json_file) as jf:
obj = json.load(jf)
# gen csv file
with open(csv_file,'w', encoding='utf-8') as f:
writer = csv.writer(f, lineterminator='\n', delimiter=',')
# set the first line
first_line = ['ITEM NUMBER',
'DEFINE',
'PREVENT',
'REPAIR']
writer.writerow(first_line)
# set fares
item_num_cnt = 0;
for o in obj:
item_num_cnt += 1
line = []
line.append(item_num_cnt)
for key in o.keys():
line.append(o[key])
writer.writerow(line)
print(csv_file, 'is generated')
if __name__ == '__main__':
main()
[
{
"DEFINE" : "It is said to be insane as a society person in TMC",
"PREVENT": "Say around in advance",
"REPAIR" : "Work firmly as a society person in other situations"
},
{
"DEFINE" : "People around me become hard to talk to me, and communication becomes insufficient",
"PREVENT": "Leave a poster saying please feel free to talk to me",
"REPAIR" : "Apologize and explain what you can talk to me"
},
{
"DEFINE" : "I get on music and make noise",
"PREVENT": "Choose calm music",
"REPAIR" : "Apologize, make it more calm music, or use an earplug"
}
]