I have a JSON file:
{
'Document':[
[{'fields': {'name': 'js/main.js', 'content': 'hello'}, 'pk': 284, 'model': 'Document'}],
[{'fields': {'name': 'css/main.css', 'content': '2'}, 'pk': 287, 'model': 'Document'}],
[{'fields': {'name': 'about_us.html', 'content': 'again hello'}, 'pk': 306, 'model': 'Document'}]],
'Package':
[{'fields': {'package_type': 'THEME', 'base_package': None, 'created_date': '2015-05-25T15:39:16.781Z', 'name': '25_may', 'rating_avg': 0.0, 'user': 2, 'rating_count': 0, 'is_published': True}, 'pk': 129, 'model': 'Package'}]
}
I want to generate directory structure based on JSON data:
25_may |- css | |- main.css |- js | |-main.js |- about_us.html
Code to achieve above:
def my_func():
package_name = response['Package'][0]['fields']['name']
for doc in response['Document']:
filename = os.path.join(package_name, doc[0]['fields']['name'])
content = doc[0]['fields']['content']
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
with open(filename, "w") as f:
f.write(content)
Is there an efficient and better way to do this?
1 Answer 1
It seems fine, but I suggest to improve some minor improvements:
filename
is not a simple file name but has directory element too, so I'd call itpath
- Instead of calling
os.path.dirname
twice, I'd call it once and cache the result in a local variable
Like this:
package_name = response['Package'][0]['fields']['name']
for doc in response['Document']:
path = os.path.join(package_name, doc[0]['fields']['name'])
basedir = os.path.dirname(filename)
if not os.path.exists(basedir):
os.makedirs(basedir)
content = doc[0]['fields']['content']
with open(filename, "w") as f:
f.write(content)
Btw, the JSON structure looks a bit odd, with several arrays with a single element. Makes me wonder why these arrays are there at all, instead of their contained objects directly.
I'd also be concerned about format changes in the JSON: your code is tightly coupled to an unintuitive structure, and if anything changes later, it might be difficult to migrate the implementation.
-
\$\begingroup\$ Thanks for your suggestion for json structure. I will change it's format. \$\endgroup\$Prashant Gaur– Prashant Gaur2015年05月26日 18:28:27 +00:00Commented May 26, 2015 at 18:28
Explore related questions
See similar questions with these tags.