繁体   English   中英

如何正确地将 csv 中包含日期的列转换为 JSON

[英]How to correctly convert the column in csv that contains the dates into JSON

在我的 csv 文件中, "ESTABLİSHMENT DATE"列由如下斜线分隔: 01/22/2012

I am converting the csv format into the JSON format, which needs to be done with pandas, but the "ESTABLİSHMENT DATE" column isn't correctly translated to JSON.

df = pd.read_csv(my_csv)
df.to_json("some_path", orient="records")

我不明白为什么它笨拙地添加了反斜杠。

"ESTABLİSHMENT DATE":"01\/22\/2012",

但是,我需要将结果写入文件,如下所示:

"ESTABLİSHMENT DATE":"01/22/2012",
  • Forward slash in json file from pandas dataframe answers why it awkwardly adds the backward slashes , and this answer shows how to use the json library to solve the issue.
    • 只要日期格式是01/22/2012/就会用\转义。
  • 要使用 pandas 将包含日期的 csv 中的列正确转换为JSON ,可以通过将'date'列转换为正确的datetime dtype时间,然后使用.to_json来完成。
    • 2012-01-22是正确的日期时间格式,但.to_json会将其转换为1327190400000 使用pd.to_datetime将正确的格式设置为%Y-%m-%d后,必须将类型设置为string
import pandas as pd

# test dataframe
df = pd.DataFrame({'date': ['01/22/2012']})

# display(df)
         date
0  01/22/2012

# to JSON
print(df.to_json(orient='records'))
[out]: [{"date":"01\/22\/2012"}]

# set the date column to a proper datetime
df.date = pd.to_datetime(df.date, format='%m/%d/%Y')

# display(df)
        date
0 2012-01-22

# to JSON
print(df.to_json(orient='records'))
[out]: [{"date":1327190400000}]

# set the date column type to string
df.date = df.date.astype(str)

# to JSON
print(df.to_json(orient='records'))
[out]: [{"date":"2012-01-22"}]

# as a single line of code
df.date = pd.to_datetime(df.date, format='%m/%d/%Y').astype(str)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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