Python Tutorial
YAML Tag | Python Type |
---|---|
null | None |
bool | bool |
int | int or long (Python 3 - int) |
float | float |
binary | str (Python 3 - bytes) |
timestamp | datetime |
omap, pairs | list of pairs |
set | set |
str | str or unicode (Python 3 - str) |
seq | list |
map | dict |
#!/usr/bin/python import yaml; print(yaml.dump([1, "two", 3.0]));Output:
$ python test_yaml.py [1, two, 3.0]
#!/usr/bin/python import yaml; print(yaml.dump([u'welcome', u'how r u?']));Output:
$ python test_yaml.py [!!python/unicode 'welcome', !!python/unicode 'how r u?']
#!/usr/bin/python import yaml; print yaml.dump({'name': 'student1', 'dept': 'computer', 'marks': [87, 96]});Output:
$ python test_yaml.py dept: computer marks: [87, 96] name: student1
#!/usr/bin/python import yaml; print(yaml.dump_all([1,2,3]));Output:
$ python test_yaml.py 1 --- 2 --- 3 ...
#!/usr/bin/python import yaml; print(yaml.safe_dump([1., "two", 3.0]));Output:
$ python test_yaml.py [1.0, two, 3.0]
#!/usr/bin/python import yaml; print(yaml.safe_dump([u'welcome', u'how r u?']));Output:
$ python test_yaml.py [welcome, 'how r u?']
#!/usr/bin/python import yaml; print(yaml.safe_dump([u'welcome', u'how r u?'], default_flow_style=False));Output:
$ python test_yaml.py - welcome - how r u?
#!/usr/bin/python import yaml; print yaml.safe_dump({'name': 'student1', 'dept': 'computer', 'marks': [87, 96]});Output:
$ python test_yaml.py dept: computer marks: [87, 96] name: student1
#!/usr/bin/python import yaml; print(yaml.safe_dump_all([1,2,3]));Output:
$ python test_yaml.py 1 --- 2 --- 3 ...
#!/usr/bin/python import yaml; f = open("file_name.txt", "w"); yaml.dump({'name': 'student1', 'dept': 'computer', 'marks': [87, 96]}, f); f.close(); print("YAML serialization is done!");Output:
$ python test_yaml.py YAML serialization is done!file_name.txt
$ cat file_name.txt dept: computer marks: [87, 96] name: student1
#!/usr/bin/python import yaml; print(yaml.dump([1,2,3], explicit_start=True));Output:
$ python test_yaml.py --- [1, 2, 3]
#!/usr/bin/python import yaml; print(yaml.dump_all([1,2,3], explicit_start=True));Output:
$ python test_yaml.py --- 1 --- 2 --- 3 ...
#!/usr/bin/python import yaml; f = open("file_name.txt", "w"); yaml.safe_dump({'name': 'student1', 'dept': 'computer', 'marks': [87, 96]}, f); f.close(); print("YAML serialization is done!");Output:
$ python test_yaml.py YAML serialization is done!file_name.txt
$ cat file_name.txt dept: computer marks: [87, 96] name: student1
#!/usr/bin/python import yaml; yaml_doc =""" - Python - Java - C - C# """ print(yaml.load(yaml_doc));Output:
$ python test_yaml.py ['Python', 'Java', 'C', 'C#']
#!/usr/bin/python import yaml; yaml_doc =""" --- name: Python description: python programming --- name: Java description: Java programming --- name: C description: C programming """ for data in yaml.load_all(yaml_doc): print data;Output:
$ python test_yaml.py {'name': 'Python', 'description': 'python programming'} {'name': 'Java', 'description': 'Java programming'} {'name': 'C', 'description': 'C programming'}
#!/usr/bin/python import yaml; yaml_doc =""" - !!python/unicode 'welcome' - !!python/unicode 'how r u?' """ print(yaml.load(yaml_doc));Output:
$ python test_yaml.py [u'welcome', u'how r u?']
#!/usr/bin/python import yaml; yaml_doc =""" - python - yaml """ print(yaml.safe_load(yaml_doc));Output:
$ python test_yaml.py ['python', 'yaml']
#!/usr/bin/python import yaml; f = open("file_name.txt", "r"); print(yaml.load(f)); f.close();Output:
$ python test_yaml.py {'dept': 'computer', 'name': 'student1', 'marks': [87, 96]}
#!/usr/bin/python import yaml; f = open("file_name.txt", "r"); print(yaml.safe_load(f)); f.close();Output:
$ python test_yaml.py {'dept': 'computer', 'name': 'student1', 'marks': [87, 96]}
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page