Is there a way to prevent pandas to_json from adding \?

2024/9/23 19:55:23

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.

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

Sample Data:

b_Updated,
Updated:09/06/2016 03:09:44,
Updated:06/29/2016 08:16:52,
Updated:09/07/2016 07:54:37,
Answer

The JSON output you obtained is indeed correct and is the right behavior.

Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings. Hence, in JSON / and \/ are equivalent.

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"}]
https://en.xdnf.cn/q/71739.html

Related Q&A

Convert SQL into json in Python [duplicate]

This question already has answers here:return SQL table as JSON in python(15 answers)Closed 8 years ago.I need to pass an object that I can convert using $.parseJSON. The query looks like this:cursor.e…

Django Middleware - How to edit the HTML of a Django Response object?

Im creating a custom middleware to django edit response object to act as a censor. I would like to find a way to do a kind of search and replace, replacing all instances of some word with one that I c…

Disable or restrict /o/applications (django rest framework, oauth2)

I am currently writing a REST API using Django rest framework, and oauth2 for authentication (using django-oauth-toolkit). Im very happy with both of them, making exactly what I want.However, I have on…

find command with exec in python subprocess gives error

Im trying to execute the following command using subprocess module (python)/usr/bin/find <filepath> -maxdepth 1 -type f -iname "<pattern>" -exec basename {} \;But, it gives the fo…

Tensorflow import error on Pycharm (Mac)

Error msg (check the screenshot picture please):ImportError: cannot import name symbol_databaseError importing tensorflow. Unless you are using bazel, you should not try to import tensorflow from its …

ssl.SSLCertVerificationError for flask application OAuth login with keycloak

I have referred a sample hello-world flask app integrated with key-cloak login from https://gist.github.com/thomasdarimont/145dc9aa857b831ff2eff221b79d179a My client-secrets.json is as follows: {"…

Need to transfer multiple files from client to server

Im recently working on a project in which Im basically making a dropbox clone. The server and client are working fine but Im having a slight issue. Im able to transfer a single file from the client to …

pyplot bar charts with individual data points

I have data from a control and treatment group. Is matplotlib able to create a bar chart where the bar height is the mean of each group overlaid with the individual data points from that group? Id lik…

python only works with sudo

My python 2.7 script works on my Ubuntu system if I call it using sudo python [filename].pyor from a bash script using sudo ./[bashscriptname].shBut if I call it from Pycharm I get oauth errors, and fr…

Forbidden (CSRF token missing or incorrect) Django error

I am very new to Django. The name of my project is rango and I have created a URL named /rango/tagger that is supposed to send an object. In my java-script, I have tried to communicate with this route …