-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_example.py
More file actions
50 lines (36 loc) · 1.53 KB
/
Copy pathjson_example.py
File metadata and controls
50 lines (36 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import maya.cmds as cmds
import json
import tempfile
# An empty dictionary that will be used to store locator_info
locator_info_dictionary = {}
# Define a list containing locator info
locator_info = (['lctr1', [0.0,0.0,0.0]], ['lctr2', [1.0,0.0,-1.0]], ['lctr3', [2.0,0.0,0.0]])
# Assign locator_info to dictionary keys
# For this example we will look at using list comprehensions
# Read documentation here... http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
locator_info_dictionary['names']= [locator_info[x][0] for x in range(len(locator_info))]
locator_info_dictionary['positions']= [locator_info[x][1] for x in range(len(locator_info))]
print locator_info_dictionary
data = locator_info_dictionary
# These functions can be used to read and write json
# Define a path to the json file. Change this to a path on your computer.
fileName = 'C:/Users/Griffy/Documents/GitHub/Python101/data/locator_info.json'
def writeJson(fileName, data):
with open(fileName, 'w') as outfile:
json.dump(data, outfile)
file.close(outfile)
def readJson(fileName):
with open(fileName, 'r') as infile:
data = (open(infile.name, 'r').read())
return data
# Now we will save to a json file
writeJson(fileName, data)
# Read the Json file
data = readJson(fileName)
info = json.loads( data )
info2 = json.dumps( data )
# Now take a look at the different data types returned by loads and dumps
print type(json.dumps( data ))
print type(json.loads( data ))
for key, value in info.iteritems():
print key, value