使用指定的数据类型将json转换为csv

时间:2019-05-14 12:05:38

标签: python json pandas csv

我想将json转换为具有指定数据类型(字符串)的csv

属性值”:“ 3-4”在CSV中转换为“ 4-三月”,而不是“ 3-4”

frame.to_csv(filename.csv,encoding ='utf-8',index = False,header = True,columns = [“ FILE NAME”,“ PROPERTY VALUE”])

如何实现?这是我尝试过的:

dirs = os.listdir(path)
frame = pandas.DataFrame()
#frame = frame.astype(str)
output_folder_time_stamp = str(datetime.datetime.now()).replace(":", " ").rsplit(".", 1)[0]
final_output_path = outputpath + str(output_folder_time_stamp)
try:

    for filename in dirs :
        subdirs=path + filename
        subdirs = os.listdir(subdirs)
        if len(subdirs)>0:
            for jsonfilename in subdirs :
                root, ext = os.path.splitext(jsonfilename)
                if ext == '.json':
                    tmp_frame = pandas.read_json(path+filename+"/"+jsonfilename)
                    frame = frame.append(tmp_frame, ignore_index=True)


            if not os.path.exists(final_output_path):
                os.makedirs(final_output_path)                                

            frame.to_csv(filename.csv',encoding='utf-8',index=False,header=True, columns=["FILE NAME","PROPERTY VALUE"])


            frame = pandas.DataFrame()
            tmp_frame=''

0 个答案:

没有答案