import json import os import pandas as pd dict_color = {'Key1':"#ff0000", 'Key2':"#ff0000", 'Key3':"#ff0000" } dict_name = {'Key1':"name1", 'Key2':"name2",'Key3':"name3" } def generate_list(work_path): ''' params work_path: files path that needs to be converted to do : generate dataframe from all stl files return : pd ''' record = pd.Dataframe(columns = ['color', 'title', 'stl']) stl_list = os.listdir(work_path) for stl in stl_list: if stl.split('.')[-1] == 'stl': df = pd.Dataframe({ 'color': dict_color[stl.split('.')[0]], 'title': dict_name[stl.split('.')[0]], 'stl': stl, },index=[0]) record = record.append(df) # record.to_csv(os.path.join(work_path, 'json.csv')) return record def generate_json(work_path): ''' params work_path: files path that needs to be converted to do : save pd to json as rules return : None ''' jsontext = {'Myjson_file':[]} record = generate_list(work_path) for index,row in record.iterrows(): jsontext['Myjson_file'].append({'color':row['color'], 'title':row['title'], 'stl':row['stl']}) jsondata = json.dumps(jsontext,indent=4,separators=(',', ': ')) f = open(os.path.join(work_path, 'MyFirst.json'), 'w') f.write(jsondata) f.close() if __name__ == '__main__': root_path = r'D:xxmodel' generate_json(root_path)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)