0

I am still facing a problem in receiving correct date formatted data from the CSV using to_json fu nction of pandas.

import pandas as pd
import json

df = pd.read_csv("C:\\Users\\shubham\\Desktop\\Output\\MasterData.csv")
df1 = df.to_json(orient='records')
print(df1)

[{"invoiceDate":"18\/08\/2019","amount":1140.87}]

I am expecting Output:- "invoiceDate":"18/08/2019"

I already tried to_json arguments:- date_format = "iso" double_precision = 10, force_ascii = True, date_unit = "ms", default_handler = None), and replace is also not working (df.replace("/","")).

shubham jain
  • 45
  • 11
  • Please share your df (no need for a csv), or more correctly, a minimal example that reproduces the error. – Itamar Mushkin Jul 31 '19 at 10:19
  • 2
    May I know Why do you want to remove `backslash`. In json `"promotionValidFrom":"10\/12\/2019"` and `"promotionValidFrom":"10/12/2019"` is same. backslash(escape character) is for precaution. – Mohamed Thasin ah Jul 31 '19 at 10:20

2 Answers2

0
import pandas as pd
def OCRExecution():
    df = pd.read_csv("C:\\Users\\shubham\\OS_task\\masterData.csv")
    df1 = df.to_json(orient='records')
    df1.replace("\","")
    return df1
OCRExecution()
0

I resolved this date format issue by replace function. Later I got to python only add on additional backward slash if our data type is date, not string.

df1 = df.to_json(orient='records',lines=True).replace('\\r\\n', " ")
David Buck
  • 3,752
  • 35
  • 31
  • 35
shubham jain
  • 45
  • 11