Hướng dẫn python text to yaml - văn bản python thành yaml

YAML là gì?

YAML (YAML Ain’t Markup Language) là một chuẩn dữ liệu kiểu serialization dành cho tất cả các ngôn ngữ. Nó được sử dụng phổ biến để tạo ra các file config cho nhiều ứng dụng, VD: như Docker Compose. (YAML Ain’t Markup Language) là một chuẩn dữ liệu kiểu serialization dành cho tất cả các ngôn ngữ. Nó được sử dụng phổ biến để tạo ra các file config cho nhiều ứng dụng, VD: như Docker Compose.

Nội dung chính

  • YAML là gì?
  • Thông tin cơ bản:
  • Cú pháp cơ bản
  • Đọc 1 file YAML sử dụng ngôn ngữ lập trình
  • Đọc file YAML trong Perl
  • Đọc file YAML trong PHP
  • Đọc file YAML trong Python
  • Giới thiệu:
  • 1. Một số câu lệnh Jinja2:
  • 2. Các bước thực hiện:

Thông tin cơ bản:

  • Cú pháp cơ bản
  • Đọc 1 file YAML sử dụng ngôn ngữ lập trình
  • Đọc file YAML trong Perl

Cú pháp cơ bản

Đọc 1 file YAML sử dụng ngôn ngữ lập trình

# Programing Languages
- PHP
- Perl
- NodeJS

# Shopping list
[milk, pumpkin pie, eggs, juice]

Đọc file YAML trong Perl

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}

Đọc file YAML trong PHP

data: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don't spit on the floor"
   So he carefully spat on the ceiling

data: >
   Wrapped text
   will be folded
   into a single
   paragraph

   Blank lines denote
   paragraph breaks

Đọc file YAML trong Python

customer:
    first_name:   Dorothy
    family_name:  Gale

Đọc 1 file YAML sử dụng ngôn ngữ lập trình

Đọc file YAML trong Perl

Đọc file YAML trong PHP

cpan YAML::XS

Đọc file YAML trong Python

#!/usr/bin/perl
#
# Read YAML Config File by vinasupport.com
#

use strict;
use warnings;
use YAML::XS 'LoadFile';
use Data::Dumper;
    
my $config = LoadFile('config.yaml');
print Dumper($config);

Đọc file YAML trong PHP

Đọc file YAML trong Python

 'cb_yaml_date'));

print_r($data);

Giới thiệu:

  • symfony/yaml

Đọc file YAML trong Python

Giới thiệu:

pip3 install PyYAML

1. Một số câu lệnh Jinja2:

#!/usr/bin/env python3
import yaml

with open("configs.yaml", 'r') as stream:
    try:
        print(yaml.safe_load(stream))
    except yaml.YAMLError as error:
        print(error)

2. Các bước thực hiện:

Hướng dẫn python text to yaml - văn bản python thành yaml

Version mới nhất: 1.2 2 phút đọc

Giới thiệu:

Jinja2 is a powerful templating language for Python. If you are in network automation business you are going to love it I guarantee it. Using Jinja2 you can build a template which uses variables instead of hard coded values, Jinja2 then automagically renders template using variable values. Variable values either comes from Python dictionary or yaml file. Jinja2 also allows you to add control statements like ‘for’ loop & ‘If’ statement to create logic in template file.

1. Một số câu lệnh Jinja2:

2. Các bước thực hiện: biến bắt đầu với {{ tên biến và kết thúc với }} example: tạo biến có tên interface {{ interface }}

Version mới nhất: 1.2 câu lệnh for bắt đầu với {% for statement %} và kết thúc với {% end %} example: {% for interface in interfaces %} … {% endfor %}

Định dạng mở rộng: .yaml, .yml Câu lệnh if bắt đầu với {% if statement %} và kết thúc với {% endif %} example: {% if interface == ‘ge-0/0/2’ %} … {% endif %}

Tổ chức: yaml.org comment starts with ‘{#’ and ends with ‘#} example: {# set description only for interface ge-0/0/2 #}

Định nghĩa cấu trúc một danh sách (list) hay mảng (array) pip install jinja2 ( cho python 2.7 ) pip3 install jinja2 ( cho python > 3.6 )

2. Các bước thực hiện:

Version mới nhất: 1.2

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}
0

Định dạng mở rộng: .yaml, .yml mở terminal hoặc command-line và start python ( ở đây tôi đang dùng terminal trên Linux ), Import packages, set environment và load jinja2 template:

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}
1

Tổ chức: yaml.org

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}
2

Định nghĩa cấu trúc một danh sách (list) hay mảng (array)interfaces.yaml file để lưu lại các thông tin cấu hình

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}
3

Định nghĩa kiểu dữ liệu key-value

# Indented Block
name: Nguyen Van A
age: 33
# Inline Block
{name: Nguyen Van A, age: 33}
4

Định nghĩa ký tự dạng chuỗi, ký tự dạng chuỗi không yêu cầu dấu quote

Định nghĩa 1 đối tượng

  • Sử dụng thư viện Perl có tên là YAML::XS để đọc bất kỳ file YAML nào. Để cài đặt module này các bạn sử dụng cài thông qua cpan
  • Đoạn code example, mà mình sử dụng để load 1 file config.yml

Để đọc file yaml trong PHP, các bạn có thể sử dụng function yaml_parse_file được PHP hỗ trợ sẵn. Đoạn code example như bên dưới.