有没有办法防止 pandas to_json添加\? [英] Is there a way to prevent pandas to_json from adding \?
问题描述
我正在尝试将熊猫数据帧发送到_json,并且我在日期方面遇到了一些问题.我得到一个附加\,以便我的记录看起来像Updated:09\/06\/2016 03:09:44
.是否可以不添加此附加\?我以为这是某种转义字符,但我无法找到与此有关的任何其他信息.
I am trying to send a pandas dataframe to_json and I am having some issues with the date. I am getting an addtional \ so that my records look like Updated:09\/06\/2016 03:09:44
. Is it possible to not have this additional \ added? I am assuming that it is an escape character of some sort but I haven't been able to find any additional information regarding this.
我一直在调整各种参数,但是我没有任何运气df[0:10].to_json('splunkJsonFormat.txt', orient='records', date_format='ISO8601')
I have been adjusting the various parameters but I havent had any luck df[0:10].to_json('splunkJsonFormat.txt', orient='records', date_format='ISO8601')
样本数据:
b_Updated,
Updated:09/06/2016 03:09:44,
Updated:06/29/2016 08:16:52,
Updated:09/07/2016 07:54:37,
推荐答案
您获得的JSON输出确实是正确的,并且是正确的行为.
The JSON output you obtained is indeed correct and is the right behavior.
在将JSON嵌入到<script>
标记中时不允许使用\/
,该标记不允许在字符串内使用</
.因此,在JSON中,/
和\/
是等效的.
Allowing \/
helps when embedding JSON in a <script>
tag, which doesn't allow </
inside strings. Hence, in JSON /
and \/
are equivalent.
一种解决方法是将日期与字符串分开,然后将其转换为更适合datetime格式的格式.
One workaround would be to separate the date from the string and convert it to a format more suitable where the datetime format is more evident.
df['b_Updated'] = df['b_Updated'].str.split(':', 1) \
.apply(lambda x: x[0] + ':' + str(pd.to_datetime(x[1])))
df.to_json(orient='records', date_format='iso')
[{"b_Updated":"Updated:2016-09-06 03:09:44"},
{"b_Updated":"Updated:2016-06-29 08:16:52"},
{"b_Updated":"Updated:2016-09-07 07:54:37"}]
这篇关于有没有办法防止 pandas to_json添加\?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!