Skip to content

Commit

Permalink
Finished steps 6 and 7 - added import_json_file.py and import_yaml_fi…
Browse files Browse the repository at this point in the history
…le.py
  • Loading branch information
swackhap committed Jan 21, 2016
1 parent a0525f9 commit b4f4865
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions a1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0, 1, 2, 3, 4, 5, 6, 7, "dog", "cat", {"attribs": [0, 1, 2, 3, 4, 5, 6], "ip_addr": "192.168.255.21"}]
19 changes: 19 additions & 0 deletions a1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- dog
- cat
- attribs:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
ip_addr: 192.168.255.21
9 changes: 8 additions & 1 deletion create_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@
list.append('cat')
list.append({})
list[-1]=dict

#Print list in raw python form
print list

#Print list in raw yaml form
print 'yaml.dump(list)'
print yaml.dump(list)

#Print list in yaml form with default flow style off--more human readable
print 'yaml.dump(list, default_flow_style=False)'
print yaml.dump(list, default_flow_style=False)

#Print list in yaml form with default flow style on
print 'yaml.dump(list, default_flow_style=True)'
print yaml.dump(list, default_flow_style=True)

#Print list in json form
print 'json.dumps(list)'
print json.dumps(list)

#Write list to yaml file in long form
#yaml filename
yaml_filename = filename_base + '.yml'
print 'Writing yaml file ' + filename_base + '.yml'
Expand All @@ -34,4 +41,4 @@
json_filename = filename_base + '.json'
print 'Writing json file ' + json_filename
with open(json_filename, "w") as f:
json.dumps(list,f)
json.dump(list,f)
13 changes: 13 additions & 0 deletions import_json_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json

#Get base filename input from user for file to be read
filename_base = raw_input("What is the base json filename to be read? :")

#Open file and read into variable "list"
print 'Opening JSON file for reading'
with open(filename_base + ".json") as f:
list = json.load(f)

#Pretty-print
print 'json.dumps(list)'
print json.dumps(list)
12 changes: 12 additions & 0 deletions import_yaml_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import yaml

#Get base filename input from user for file to be read
filename_base = raw_input("What is the base yaml filename to be read? :")

#Open file and read into variable "list"
with open(filename_base + ".yml") as f:
list = yaml.load(f)

#Pretty-print
print 'yaml.dump(list,default_flow_style=False)'
print yaml.dump(list,default_flow_style=False)

0 comments on commit b4f4865

Please sign in to comment.