how to remove a back slash from a JSON file

2024/10/2 20:32:40

I want to create a json file like this:

{"946705035":4,"946706692":4 ...}

I am taking a column that only contains Unix Timestamp and group them.

result = data['Last_Modified_Date_unixtimestamp_no_time'].value_counts()In [21]: result.head()
Out[21]: 
1508284800    131
1508716800    106
1508371200    101
1508457600     99
1508630400     96
Name: Last_Modified_Date_unixtimestamp_no_time, dtype: int64

transform to a dict

result = result.to_dict()
result
'''
{1507161600: 1,1507852800: 1,1508198400: 64,1508284800: 131,...1535155200: 1,1535241600: 1}
'''import json
result = json.dumps(result)with open('result.json', 'w') as fp:json.dump(result, fp, indent=4)

result enter image description here this is the data structure that I expected

{"946705035":4,"946706692":4}
Answer

You're dumping the JSON twice, which causes quotes to be escaped on the second dump. (After the first json.dumps the result is only a string, so you're just dumping a string instead of a dict again)

import json
# result = json.dumps(result)with open('result.json', 'w') as fp:json.dump(result, fp, indent=4)

Or remove the second dump:

import json
result = json.dumps(result)with open('result.json', 'w') as fp:# json.dump(result, fp, indent=4)print(result, file=fp)
https://en.xdnf.cn/q/70813.html

Related Q&A

Can sockets be used to connect multiple computers on different networks in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

python logging close and application exit

I am using the logging module in an application and it occurred to me that it would be neat if the logging module supported a method which would gracefully close file handles etc and then close the app…

LookupError: unknown encoding: cp0

I am on window 7, python 2.7.2, pandas 0.11.0, django 1.4, wsgi and apache 2.2. I have a pandas script that works fine if I run it directly with python and also works in ipython with %run. However, w…

Cant activate Python venv in Windows 10

I created a virtual environment with python -m venv myenv from the command prompt, but I dont know how to activate it. I tried executing activate.bat from the command prompt but it does not activate.In…

Opening file path not working in python [duplicate]

This question already has answers here:open() gives FileNotFoundError / IOError: [Errno 2] No such file or directory(11 answers)Closed last year.I am writing a database program and personica is my test…

Cython: Segmentation Fault Using API Embedding Cython to C

Im trying to embed Cython code into C following Oreilly Cython book chapter 8. I found this paragraph on Cythons documentation but still dont know what should I do:If the C code wanting to use these fu…

Computing AUC and ROC curve from multi-class data in scikit-learn (sklearn)?

I am trying to use the scikit-learn module to compute AUC and plot ROC curves for the output of three different classifiers to compare their performance. I am very new to this topic, and I am struggli…

Nested dictionary

I am working on some FASTA-like sequences (not FASTA, but something I have defined thats similar for some culled PDB from the PISCES server).I have a question. I have a small no of sequences called nCa…

How to create a Python script to automate software installation? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Using __str__() method in Django on Python 2

I am Learning Django using the Django project tutorial. Since I use python 2.7 I am unable to implement the following in python 2.7:from django.db import modelsclass Question(models.Model): # ...def _…