Json Find Key Contain Some Char And Write Key And Value To New Json File With Python
I need find if key contain dash, than need to get this key and value to new json file.
this is my code:
#coding=utf-8
import os
import sys
import json
import fileinput
file_path = sys.argv[1]
file = open(file_path, 'r')
content = file.read()
dict = json.loads(content, encoding="utf-8")
output = "{"
for key in dict:
if key.find("-") != -1:
output = output + "%s: %s" % (key, unicode(dict[key]).encode('utf8'))
print output
output = output + "}"
output = json.dumps(json.loads(output, encoding="utf-8"), indent=4, separators=(', ',': '), ensure_ascii=False).encode('utf8')
file_name = os.path.basename(file_path)
sort_file = open(file_name, 'a')
sort_file.write(output)
sort_file.close()
output file is some kind of:
u'login': u".//input[@placeholder='Email/ \u624b\u6a5f\u865f\u78bc/
Is any way to convert content_dict[key] to utf-8 char not like "\u78bc"? and have any good way to find key contain some char and write to new json file?
Answer
You are using Python 2, and want to be able to read and write json files that contain non-ascii characters.
The easiest way to do this is to perform your processing with unicode only, performing file IO in binary mode and converting raw bytes to json after decoding to unicode when reading, and encoding json to bytes before writing to file.
The code should look something like this:
file_path = sys.argv[1]
# Read data as bytes
with open(file_path, 'rb') as f:
raw_data = f.read()
# Decode bytes to unicode, then convert from json.
dict_ = json.loads(raw_data.decode('utf-8'))
output = {}
for key, value in dict_.iteritems():
# Using the in operator is the Pythonic way to check
# if a character is in a string.
if "-" in key:
output[key] = value
print output
file_name = os.path.basename(file_path)
with open(file_name, 'ab') as f:
j = json.dumps(output, indent=4, separators=(', ', ': '), ensure_ascii=False)
# Encode json unicode string before writing to file.
f.write(j.encode('utf-8'))
In this code I've used the with statement to handle closing open files automatically.
I have also collected the data to be written in a dictionary rather than in a string. Building json strings manually can often be a cause of errors.
Switching to Python 3 would remove the need for separate encoding and conversion steps and generally simplify handling non-ascii data.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module