简体   繁体   中英

Pandas 'to_json' function returning wrong format of date

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("/","")).

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()

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', " ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM