Hướng dẫn update yaml file python - cập nhật tệp yaml python
The ruamel.yaml package was specifically enhanced (by me starting from PyYAML) to do this kind of round-trip, programmatic, updating. Show
If you start with (please note I removed the extra initial spaces):
and run:
The output will be:
The ordering of mapping keys ( 9, 0 and 1), the style and the comments are preserved without any further specific action.the
comments are preserved without any further specific action.Instead of having the indent and block sequence indent guessed, you can do a manual traditional load, and set the indent values yourself:
If you look at the history of this answer, you can see how to do this with a more limited, PyYAML like, API. 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 ShowShow
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.
Nội dung chính ShowThông tin cơ bản: # Programing Languages - PHP - Perl - NodeJS # Shopping list [milk, pumpkin pie, eggs, juice] Cú pháp cơ bản # Indented Block name: Nguyen Van A age: 33 # Inline Block {name: Nguyen Van A, age: 33} Đọc 1 file YAML sử dụng ngôn ngữ lập trình 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 Perl customer: first_name: Dorothy family_name: Gale 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 cpan YAML::XS Đọc file YAML trong Perl #!/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 1 file YAML sử dụng ngôn ngữ lập trìnhĐọc file YAML trong Perl 0Đọc file YAML trong PHP
Đọc file YAML trong PerlĐọc file YAML trong PHP 1Đọc file YAML trong Python 2Giới thiệu: 1. Một số câu lệnh Jinja2: 2 phút đọc Đọc file YAML trong PHP3 Đọc file YAML trong PythonGiới thiệu: 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 }} 1. Một số câu lệnh Jinja2: 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 %} 2. Các bước thực hiện: 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 %} Version mới nhất: 1.2 comment starts with ‘{#’ and ends with ‘#} example: {# set description only for interface ge-0/0/2 #} Định dạng mở rộng: .yaml, .yml pip install jinja2 ( cho python 2.7 ) pip3 install jinja2 ( cho python > 3.6 ) Giới thiệu:**Bước-1: ** Tạo file Jinja2 template với tên interfaces.j2. Thêm nội dung vào file interfaces.j2, ở đây tôi tạo một mẫu xml nhưng nó có thể là bất kì format: 4Bước-2: 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: 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: 5Ngoài ra bạn có thể render bằng cách sau: 6Bước-3: Bạn có thể configuration đến file thay cho biến config ở trên để khi cần thay đổi không phải vào sửa lại code: Tạo interfaces.yaml file để lưu lại các thông tin cấu hìnhinterfaces.yaml file để lưu lại các thông tin cấu hình 7Đọc yaml file trong Python 8Vậy là đến đây bạn đã biết sử dụng Jinja2 template - Yaml File - Python cho network automation. Tham Khảo:
All rights reserved |