0

I would like to convert json to csv with specified datatype (string)

PROPERTY VALUE": "3-4" converting as "4-Mar" in CSV instead of "3-4"

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

How can I achieve it? This is what I tried:

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=''
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • look at the [docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html) specifically at convert_dates and dtype – Nullman May 14 '19 at 12:08
  • can you open the same csv file in a plain text editor and confirm. maybe the spreadsheet with which you are opening is parsing it to date format. – Shijith May 14 '19 at 12:21
  • This will convert your json to CSV: https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv Once you have it in CSV/DF, it's easy to convert a series to a datatype with: df["seriesName"].map(str) – Anthony R May 14 '19 at 13:32

0 Answers0