Mã nguồn: lib/configParser.py Lib/configparser.py Show
Mô -đun này cung cấp lớp >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 thực hiện ngôn ngữ cấu hình cơ bản cung cấp một cấu trúc tương tự như những gì mà tìm thấy trong các tệp Microsoft Windows Ini. Bạn có thể sử dụng điều này để viết các chương trình Python có thể được người dùng cuối tùy chỉnh một cách dễ dàng. Ghi chú Thư viện này không diễn giải hoặc viết các tiền tố loại giá trị được sử dụng trong phiên bản mở rộng Windows Registry của Cú pháp INI. Xem thêm Mô -đun>>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'0 Toml là một định dạng được chỉ định tốt cho các tệp cấu hình ứng dụng. Nó được thiết kế đặc biệt để trở thành một phiên bản cải tiến của INI. Mô -đun>>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'1 Hỗ trợ tạo ngôn ngữ mini giống như shell shell cũng có thể được sử dụng cho các tệp cấu hình ứng dụng. Mô -đun>>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'2 Mô -đun >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'2 thực hiện một tập hợp con của cú pháp JavaScript đôi khi được sử dụng để cấu hình, nhưng không hỗ trợ nhận xét. Bắt đầu nhanh¶Hãy để một tệp cấu hình rất cơ bản trông như thế này: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no Cấu trúc của các tệp INI được mô tả trong phần sau. Về cơ bản, tệp bao gồm các phần, mỗi phần chứa các khóa có giá trị. >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'4 Các lớp có thể đọc và viết các tệp đó. Hãy để bắt đầu bằng cách tạo tệp cấu hình ở trên theo chương trình. >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ... Như bạn có thể thấy, chúng ta có thể xử lý một trình phân tích cú pháp cấu hình giống như một từ điển. Có sự khác biệt, được phác thảo sau, nhưng hành vi này rất gần với những gì bạn mong đợi từ một từ điển. Bây giờ chúng tôi đã tạo và lưu một tệp cấu hình, hãy để đọc lại và khám phá dữ liệu mà nó lưu giữ. >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes' Như chúng ta có thể thấy ở trên, API khá đơn giản. Một chút ma thuật duy nhất liên quan đến phần >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'5 cung cấp các giá trị mặc định cho tất cả các phần khác 1. Lưu ý rằng các khóa trong các phần không nhạy cảm trường hợp và được lưu trữ trong chữ thường 1. Có thể đọc một số cấu hình thành một >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9, trong đó cấu hình được thêm gần đây nhất có mức độ ưu tiên cao nhất. Bất kỳ khóa xung đột nào được lấy từ cấu hình gần đây hơn trong khi các khóa hiện có trước đây được giữ lại. >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no' Hành vi này tương đương với cuộc gọi >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'7 với một số tệp được truyền đến tham số tên tệp. Dữ liệu được hỗ trợCấu hình phân tích cú pháp không đoán các kiểu dữ liệu của các giá trị trong các tệp cấu hình, luôn lưu trữ chúng trong nội bộ dưới dạng chuỗi. Điều này có nghĩa là nếu bạn cần các kiểu dữ liệu khác, bạn nên tự chuyển đổi: >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.0 Vì nhiệm vụ này rất phổ biến, các trình phân tích cú pháp cấu hình cung cấp một loạt các phương pháp getter tiện dụng để xử lý số nguyên, phao và booleans. Cái cuối cùng là thú vị nhất bởi vì chỉ cần chuyển giá trị cho >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'8 sẽ không tốt vì >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'9 vẫn là >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00. Đây là lý do tại sao trình phân tích cú pháp cấu hình cũng cung cấp >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.01. Phương pháp này không nhạy cảm trường hợp và nhận ra các giá trị boolean từ ____ 42/________ 43, ________ 44/________ 45, ________ 46/________ 47 và ________ 48/________ 49 1. Ví dụ:: >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True Ngoài >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.01, trình phân tích cú pháp cấu hình cũng cung cấp các phương thức tương đương >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True1 và >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True2. Bạn có thể đăng ký bộ chuyển đổi của riêng bạn và tùy chỉnh những cái được cung cấp. 1 Giá trị dự phòngCũng như từ điển, bạn có thể sử dụng phương thức Phần >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 để cung cấp các giá trị dự phòng: >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc' Xin lưu ý rằng các giá trị mặc định có ưu tiên hơn các giá trị dự phòng. Chẳng hạn, trong ví dụ của chúng tôi, khóa >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True4 chỉ được chỉ định trong phần >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True5. Nếu chúng tôi cố gắng lấy nó từ phần >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True6, chúng tôi sẽ luôn nhận được mặc định, ngay cả khi chúng tôi chỉ định dự phòng: >>> topsecret.get('CompressionLevel', '3') '9' Một điều nữa cần lưu ý là phương pháp >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 cấp độ phân tích cú pháp cung cấp một giao diện tùy chỉnh, phức tạp hơn, được duy trì để tương thích ngược. Khi sử dụng phương thức này, giá trị dự phòng có thể được cung cấp thông qua đối số chỉ từ khóa >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True8: >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters' Đối số >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True8 tương tự có thể được sử dụng với các phương thức >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True1, >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True2 và >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.01, ví dụ: >>> 'BatchMode' in topsecret False >>> topsecret.getboolean('BatchMode', fallback=True) True >>> config['DEFAULT']['BatchMode'] = 'no' >>> topsecret.getboolean('BatchMode', fallback=True) False Cấu trúc tệp INI được hỗ trợTệp cấu hình bao gồm các phần, mỗi phần được dẫn trước là tiêu đề >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'3, theo sau là các mục nhập khóa/giá trị được phân tách bằng một chuỗi cụ thể ( >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'4 hoặc >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'5 theo mặc định 1). Theo mặc định, tên phần nhạy cảm với trường hợp nhưng các khóa không phải là 1. Dẫn đầu và dấu vết trắng được xóa khỏi các khóa và giá trị. Các giá trị có thể được bỏ qua nếu trình phân tích cú pháp được cấu hình để cho phép 1, trong trường hợp đó, dấu phân cách khóa/giá trị cũng có thể bị bỏ lại. Các giá trị cũng có thể trải dài nhiều dòng, miễn là chúng được thụt vào sâu hơn dòng đầu tiên của giá trị. Tùy thuộc vào chế độ phân tích cú pháp, các dòng trống có thể được coi là các phần của các giá trị đa dòng hoặc bị bỏ qua. Theo mặc định, một tên phần hợp lệ có thể là bất kỳ chuỗi nào không chứa ‘\ n, hoặc‘]. Để thay đổi điều này, xem >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'6. Các tệp cấu hình có thể bao gồm các nhận xét, tiền tố bởi các ký tự cụ thể ( >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'7 và >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'8 theo mặc định 1). Nhận xét có thể tự xuất hiện trên một dòng trống, có thể thụt vào. 1 Ví dụ: >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...0 Nội suy các giá trịTrên đầu chức năng cốt lõi, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 hỗ trợ phép nội suy. Điều này có nghĩa là các giá trị có thể được xử lý trước khi trả lại chúng từ các cuộc gọi >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3. classconfigparser.basicinterpolation¶ configparser.BasicInterpolation¶ Việc triển khai mặc định được sử dụng bởi >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9. Nó cho phép các giá trị chứa các chuỗi định dạng đề cập đến các giá trị khác trong cùng một phần hoặc các giá trị trong phần mặc định đặc biệt 1. Các giá trị mặc định bổ sung có thể được cung cấp khi khởi tạo. Ví dụ: >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...1 Nội suy các giá trị Trên đầu chức năng cốt lõi, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 hỗ trợ phép nội suy. Điều này có nghĩa là các giá trị có thể được xử lý trước khi trả lại chúng từ các cuộc gọi >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3. classconfigparser.basicinterpolation¶configparser.ExtendedInterpolation¶ Việc triển khai mặc định được sử dụng bởi >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9. Nó cho phép các giá trị chứa các chuỗi định dạng đề cập đến các giá trị khác trong cùng một phần hoặc các giá trị trong phần mặc định đặc biệt 1. Các giá trị mặc định bổ sung có thể được cung cấp khi khởi tạo. Trong ví dụ trên, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 với phép nội suy được đặt thành >>> topsecret.get('CompressionLevel', '3') '9'3 sẽ giải quyết >>> topsecret.get('CompressionLevel', '3') '9'4 thành giá trị của >>> topsecret.get('CompressionLevel', '3') '9'5 ( >>> topsecret.get('CompressionLevel', '3') '9'6 trong trường hợp này). >>> topsecret.get('CompressionLevel', '3') '9'7 có hiệu lực sẽ giải quyết thành >>> topsecret.get('CompressionLevel', '3') '9'8. Tất cả các phép nội suy được thực hiện theo yêu cầu nên các khóa được sử dụng trong chuỗi tài liệu tham khảo không phải được chỉ định theo bất kỳ thứ tự cụ thể nào trong tệp cấu hình. >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...2 Với >>> topsecret.get('CompressionLevel', '3') '9'9 được đặt thành >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'0, trình phân tích cú pháp sẽ trả về >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'1 dưới dạng giá trị của >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'2 và >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'3 là giá trị của >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'4. >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...3 classconfigparser.extendsinterpolation¶Một trình xử lý thay thế cho phép nội suy thực hiện cú pháp nâng cao hơn, được sử dụng ví dụ trong Ví dụ: cấu hình được chỉ định ở trên với phép nội suy cơ bản, sẽ trông như thế này với phép nội suy mở rộng: Các giá trị từ các phần khác cũng có thể được tìm nạp: Truy cập giao thức ánh xạ truy cập
>>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...02 để nó nguyên vẹn, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ... 03 không bao giờ trả lại nó.>>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...04 - Đối số thứ hai không phải là giá trị dự phòng. Tuy nhiên, lưu ý rằng các phương thức >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 cấp độ phần tương thích với giao thức ánh xạ và API Cấu hình cổ điển. >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...06 tương thích với giao thức ánh xạ (trả về danh sách các cặp phần_name, Phần_Proxy bao gồm cả mặc định). Tuy nhiên, phương pháp này cũng có thể được gọi bằng các đối số: >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...07. Cuộc gọi sau trả về một danh sách tùy chọn, các cặp giá trị cho một >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...08 được chỉ định, với tất cả các phép nội suy được mở rộng (trừ khi >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...09 được cung cấp).
Tùy chỉnh nâng cao hơn có thể đạt được bằng cách ghi đè các giá trị mặc định của các thuộc tính phân tích cú pháp này. Các mặc định được xác định trên các lớp, vì vậy chúng có thể bị ghi đè bởi các lớp con hoặc bằng gán thuộc tính. Configparser.boolean_states¶BOOLEAN_STATES¶Theo mặc định khi sử dụng >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.01, trình phân tích cú pháp Cấu hình hãy xem xét các giá trị sau >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00: >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.08, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.02, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.06, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.04 và các giá trị sau >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...16: >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.09, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.03, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.07, >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.05. Bạn có thể ghi đè điều này bằng cách chỉ định một từ điển tùy chỉnh của chuỗi và kết quả boolean của chúng. Ví dụ: >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...9 Các cặp Boolean điển hình khác bao gồm ________ 169/________ 170 hoặc ________ 171/________ 172. ConfigParser.optionxform(option)optionxform(option)Phương thức này biến đổi tên tùy chọn trên mỗi lần đọc, nhận hoặc đặt hoạt động. Mặc định chuyển đổi tên thành chữ thường. Điều này cũng có nghĩa là khi một tệp cấu hình được viết, tất cả các khóa sẽ là chữ thường. Ghi đè phương pháp này nếu điều đó không phù hợp. Ví dụ: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'0 Ghi chú Chức năng tùy chọnxform chuyển đổi tên tùy chọn thành một hình thức chính tắc. Đây phải là một hàm idempotent: nếu tên đã ở dạng chính tắc, nó sẽ được trả về không thay đổi. Configparser.sectcre¶SECTCRE¶Một biểu thức được biên dịch thường xuyên được sử dụng để phân tích các tiêu đề phần. Mặc định khớp với >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'3 với tên >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...74. Whitespace được coi là một phần của tên phần, do đó >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...75 sẽ được đọc dưới dạng phần tên >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...76. Ghi đè thuộc tính này nếu điều đó không phù hợp. Ví dụ: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'1 Ghi chú Chức năng tùy chọnxform chuyển đổi tên tùy chọn thành một hình thức chính tắc. Đây phải là một hàm idempotent: nếu tên đã ở dạng chính tắc, nó sẽ được trả về không thay đổi. Configparser.sectcre¶Một biểu thức được biên dịch thường xuyên được sử dụng để phân tích các tiêu đề phần. Mặc định khớp với >>> topsecret.get('Port') '50022' >>> topsecret.get('CompressionLevel') '9' >>> topsecret.get('Cipher') >>> topsecret.get('Cipher', '3des-cbc') '3des-cbc'3 với tên >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...74. Whitespace được coi là một phần của tên phần, do đó >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...75 sẽ được đọc dưới dạng phần tên >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...76. Ghi đè thuộc tính này nếu điều đó không phù hợp. Ví dụ: Mặc dù các đối tượng ConfigParser cũng sử dụng thuộc tính >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...77 để nhận dạng các dòng tùy chọn, nhưng nó không khuyến nghị ghi đè nó vì điều đó sẽ can thiệp vào các tùy chọn Constructor cho phép_no_value và Delimiters. >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'2 Ví dụ API cũ >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'3 Chủ yếu là do các mối quan tâm tương thích ngược, >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'4 cũng cung cấp API di sản với các phương pháp rõ ràng ____ 156/________ 180. Mặc dù có các trường hợp sử dụng hợp lệ cho các phương pháp được nêu dưới đây, nhưng quyền truy cập giao thức ánh xạ được ưu tiên cho các dự án mới. API Legacy đôi khi nâng cao hơn, mức độ thấp và hoàn toàn trực quan. >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'4 Một ví dụ về ghi vào tệp cấu hình: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'5 Một ví dụ về đọc lại tệp cấu hình:Để được nội suy, hãy sử dụng>>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9: configparser.ConfigParser(defaults=None, dict_type=dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation(), converters={})¶ Giá trị mặc định có sẵn trong cả hai loại cấu hình. Chúng được sử dụng trong phép nội suy nếu một tùy chọn được sử dụng không được xác định ở nơi khác. ConfigparSer Đối tượng Khi nghiêm ngặt là >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00 (mặc định), trình phân tích cú pháp đã giành được cho phép bất kỳ phần hoặc tùy chọn nào sao chép trong khi đọc từ một nguồn duy nhất (tệp, chuỗi hoặc từ điển), tăng >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...83 hoặc >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...84. Khi trống_lines_in_values là >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...16 (mặc định: >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00), mỗi dòng trống đánh dấu phần cuối của một tùy chọn. Mặt khác, các dòng trống bên trong của một tùy chọn đa dòng được giữ như một phần của giá trị. Khi allow_no_value là >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00 (mặc định: >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...16), các tùy chọn không có giá trị được chấp nhận; Giá trị được giữ cho những thứ này là >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'0 và chúng được nối tiếp mà không có dấu phân cách kéo dài. Khi Default_Section được đưa ra, nó chỉ định tên cho phần đặc biệt giữ các giá trị mặc định cho các phần khác và mục đích nội suy (thường được đặt tên là >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...38). Giá trị này có thể được truy xuất và thay đổi trong thời gian chạy bằng thuộc tính phiên bản >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...91. Hành vi nội suy có thể được tùy chỉnh bằng cách cung cấp một trình xử lý tùy chỉnh thông qua đối số nội suy. >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'0 có thể được sử dụng để tắt hoàn toàn phép nội suy, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...45 cung cấp một biến thể nâng cao hơn lấy cảm hứng từ >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'5. Thêm về chủ đề trong phần tài liệu chuyên dụng. Tất cả các tên tùy chọn được sử dụng trong phép nội suy sẽ được truyền qua phương thức >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...95 giống như bất kỳ tham chiếu tên tùy chọn nào khác. Ví dụ: sử dụng triển khai mặc định của >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...95 (chuyển đổi tên tùy chọn thành chữ thường), các giá trị >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...97 và >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...98 tương đương. Khi bộ chuyển đổi được cung cấp, nó phải là một từ điển trong đó mỗi khóa biểu thị tên của một bộ chuyển đổi loại và mỗi giá trị là một cuộc gọi thực hiện chuyển đổi từ chuỗi sang kiểu dữ liệu mong muốn. Mỗi bộ chuyển đổi đều có phương thức >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...99 tương ứng trên các proxy đối tượng phân tích cú pháp. Đã thay đổi trong phiên bản 3.2: allow_no_value, delimiter, bình luận_prefixes, nghiêm ngặt, trống_lines_in_values, default_section và nội suy đã được thêm vào.allow_no_value, delimiters, comment_prefixes, strict, empty_lines_in_values, default_section and interpolation were added. Thay đổi trong phiên bản 3.5: Đối số bộ chuyển đổi đã được thêm vào.The converters argument was added. Thay đổi trong phiên bản 3.7: Đối số mặc định được đọc với >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...14, providing consistent behavior across the parser: non-string keys and values are implicitly converted to strings. Đã thay đổi trong phiên bản 3.8: Dict_type mặc định là >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...15, since it now preserves insertion order. mặc định ()()¶ Trả về một từ điển chứa các mặc định toàn phiên bản. các phần () ¶()¶Trả lại một danh sách các phần có sẵn; Phần mặc định không được bao gồm trong danh sách. add_section (phần) ¶(section)¶Thêm một phần có tên phần vào thể hiện. Nếu một phần theo tên đã cho đã tồn tại, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...83 sẽ được nêu ra. Nếu tên phần mặc định được thông qua, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...01 sẽ được nâng lên. Tên của phần phải là một chuỗi; Nếu không, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'04 được nâng lên. Đã thay đổi trong phiên bản 3.2: Tên phần không chuỗi tăng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'04. has_section (phần) ¶(section)¶ Cho biết liệu phần được đặt tên có mặt trong cấu hình hay không. Phần mặc định không được thừa nhận. Tùy chọn (phần)(section)¶Trả về một danh sách các tùy chọn có sẵn trong phần được chỉ định. has_option (phần, tùy chọn) ¶(section, option)¶Nếu phần đã cho tồn tại và chứa tùy chọn đã cho, hãy trả về >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00; Nếu không, hãy trả lại >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...16. Nếu phần được chỉ định là >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'0 hoặc một chuỗi trống, mặc định sẽ được giả định. Đọc (tên tệp, mã hóa = không) ¶(filenames, encoding=None)¶ Cố gắng đọc và phân tích một tên tệp có thể sử dụng được, trả lại một danh sách các tên tệp được phân tích thành công. Nếu tên tệp là một chuỗi, đối tượng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'09 hoặc đối tượng giống như đường dẫn, nó được coi là một tên tệp duy nhất. Nếu một tệp có tên trong tên tệp không thể được mở, tệp đó sẽ bị bỏ qua. Điều này được thiết kế để bạn có thể chỉ định các vị trí tệp cấu hình tiềm năng (ví dụ: thư mục hiện tại, thư mục nhà của người dùng và một số thư mục toàn hệ thống) và tất cả các tệp cấu hình hiện có trong ITEBLE có thể được đọc.path-like object, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify an iterable of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the iterable will be read. Nếu không có tệp nào có tên tồn tại, phiên bản >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 sẽ chứa một bộ dữ liệu trống. Một ứng dụng yêu cầu các giá trị ban đầu được tải từ tệp sẽ tải tệp hoặc tệp cần thiết bằng cách sử dụng >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...29 trước khi gọi >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'12 cho bất kỳ tệp tùy chọn nào: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'6 Mới trong phiên bản 3.2: Tham số mã hóa. Trước đây, tất cả các tệp đã được đọc bằng cách sử dụng mã hóa mặc định cho >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'13. Mới trong phiên bản 3.6.1: Tham số FileName chấp nhận một đối tượng giống như đường dẫn.The filenames parameter accepts a path-like object. Mới trong Phiên bản 3.7: Tham số FileName chấp nhận đối tượng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'09 object. read_file (f, nguồn = không) ¶(f, source=None)¶ Đọc và phân tích dữ liệu cấu hình từ F phải là chuỗi unicode có thể đạt được (ví dụ: các tệp được mở ở chế độ văn bản). Nguồn đối số tùy chọn Chỉ định tên của tệp đang được đọc. Nếu không được đưa ra và F có thuộc tính >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'15, được sử dụng cho nguồn; Mặc định là >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'16. Mới trong phiên bản 3.2: Thay thế >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'17. read_string (chuỗi, nguồn = '') ¶(string, source=' Dữ liệu cấu hình phân tích từ một chuỗi. Nguồn đối số tùy chọn chỉ định một tên cụ thể theo ngữ cảnh của chuỗi được truyền. Nếu không được đưa ra, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'18 được sử dụng. Đây thường là một đường dẫn hệ thống tập tin hoặc URL. Mới trong phiên bản 3.2. read_dict (từ điển, nguồn = '') ¶(dictionary, source='Cấu hình tải từ bất kỳ đối tượng nào cung cấp phương thức >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'19 giống như Dict. Các khóa là tên phần, giá trị là từ điển có khóa và giá trị nên có trong phần. Nếu loại từ điển được sử dụng bảo tồn thứ tự, các phần và các khóa của chúng sẽ được thêm vào theo thứ tự. Các giá trị được tự động chuyển đổi thành chuỗi. Nguồn đối số tùy chọn chỉ định một tên cụ thể theo ngữ cảnh của từ điển được truyền. Nếu không được đưa ra, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'20 được sử dụng. Phương pháp này có thể được sử dụng để sao chép trạng thái giữa các trình phân tích cú pháp. Mới trong phiên bản 3.2. read_dict (từ điển, nguồn = '') ¶(section, option, *, raw=False, vars=None[, fallback])¶Cấu hình tải từ bất kỳ đối tượng nào cung cấp phương thức >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'19 giống như Dict. Các khóa là tên phần, giá trị là từ điển có khóa và giá trị nên có trong phần. Nếu loại từ điển được sử dụng bảo tồn thứ tự, các phần và các khóa của chúng sẽ được thêm vào theo thứ tự. Các giá trị được tự động chuyển đổi thành chuỗi. Nguồn đối số tùy chọn chỉ định một tên cụ thể theo ngữ cảnh của từ điển được truyền. Nếu không được đưa ra, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'20 được sử dụng. Phương pháp này có thể được sử dụng để sao chép trạng thái giữa các trình phân tích cú pháp.Arguments raw, vars and fallback are keyword only to protect users from trying to use the third argument as the fallback fallback (especially when using the mapping protocol). Nhận (Phần, Tùy chọn, *, RAW = FALSE, VARS = none [, dự phòng]) ¶(section, option, *, raw=False, vars=None[, fallback])¶Nhận một giá trị tùy chọn cho phần được đặt tên. Nếu Vars được cung cấp, nó phải là một từ điển. Tùy chọn được tra cứu trong Vars (nếu được cung cấp), phần và mặc định theo thứ tự đó. Nếu khóa không được tìm thấy và dự phòng được cung cấp, nó được sử dụng làm giá trị dự phòng. >>> config.get('bitbucket.org', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'0 có thể được cung cấp dưới dạng giá trị dự phòng. Tất cả các phép nội suy >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'22 được mở rộng trong các giá trị trả về, trừ khi đối số thô là đúng. Các giá trị cho các khóa nội suy được tra cứu theo cách tương tự như tùy chọn.(section, option, *, raw=False, vars=None[, fallback])¶ Thay đổi trong phiên bản 3.2: Đối số RAW, VAR và dự phòng chỉ là từ khóa để bảo vệ người dùng khỏi cố gắng sử dụng đối số thứ ba như dự phòng dự phòng (đặc biệt là khi sử dụng giao thức ánh xạ). getint (phần, tùy chọn, *, raw = false, vars = none [, fallback]) ¶(section, option, *, raw=False, vars=None[, fallback])¶Một phương thức tiện lợi ép buộc tùy chọn trong phần được chỉ định cho một số nguyên. Xem >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 để giải thích về RAW, VARS và dự phòng. getFloat (phần, tùy chọn, *, raw = false, vars = none [, dự phòng]) ¶(raw=False, vars=None)¶ items(section, raw=False, vars=None) Một phương thức tiện lợi ép buộc tùy chọn trong phần được chỉ định thành số điểm nổi. Xem >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 để giải thích về RAW, VARS và dự phòng. getBoolean (phần, tùy chọn, *, raw = false, vars = none [, dự phòng]) ¶ Một phương thức tiện lợi ép buộc tùy chọn trong phần được chỉ định thành giá trị boolean. Lưu ý rằng các giá trị được chấp nhận cho tùy chọn là Khi phần không được đưa ra, hãy trả lại danh sách các cặp Phần_NAME, Phần_Proxy, bao gồm cả mặc định. Mặt khác, trả lại một danh sách tên, các cặp giá trị cho các tùy chọn trong phần đã cho. Các đối số tùy chọn có ý nghĩa tương tự như đối với phương pháp>>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3.(fileobject, space_around_delimiters=True)¶ Đã thay đổi trong phiên bản 3.8: Các mục có mặt trong VAR không còn xuất hiện trong kết quả. Hành vi trước đây trộn các tùy chọn trình phân tích cú pháp thực tế với các biến được cung cấp để nội suy.file object, which must be opened in text mode (accepting strings). This representation can be parsed by a future >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'12 call. If space_around_delimiters is true, delimiters between keys and values are surrounded by spaces. Đặt (phần, tùy chọn, giá trị) ¶ Nếu phần đã cho tồn tại, đặt tùy chọn đã cho thành giá trị được chỉ định; Nếu không, tăng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'38. tùy chọn và giá trị phải là chuỗi; Nếu không, >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'04 được nâng lên. viết (fileObject, space_around_delimiter = true) ¶(section, option)¶ Viết một biểu diễn của cấu hình vào đối tượng tệp được chỉ định, phải được mở trong chế độ văn bản (chấp nhận chuỗi). Đại diện này có thể được phân tích cú pháp bởi một cuộc gọi >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'12 trong tương lai. Nếu space_around_delimiters là đúng, các phân định giữa các khóa và giá trị được bao quanh bởi không gian. Ghi chú(section)¶ Nhận xét trong tệp cấu hình ban đầu không được bảo tồn khi viết lại cấu hình. Những gì được coi là một nhận xét, phụ thuộc vào các giá trị đã cho cho bình luận_prefix và inline_comment_prefix. remove_option (phần, tùy chọn) ¶(option)¶Hủy bỏ tùy chọn được chỉ định khỏi phần được chỉ định. Nếu phần không tồn tại, hãy tăng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'38. Nếu tùy chọn tồn tại để được xóa, hãy trả về >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) 9.00; Nếu không, hãy trả lại >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...16. Bạn không cần phải phân nhóm trình phân tích cú pháp để sử dụng phương thức này, bạn cũng có thể đặt nó trên một thể hiện, thành một hàm có đối số chuỗi và trả về một chuỗi. Ví dụ, đặt nó thành >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'46 sẽ làm cho tên tùy chọn Sensitive: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'7 Lưu ý rằng khi đọc các tệp cấu hình, khoảng trắng xung quanh các tên tùy chọn bị tước trước khi >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...95 được gọi. readfp (fp, filename = none) ¶(fp, filename=None)¶ Thay thế từ phiên bản 3.2: sử dụng >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...29 instead. Đã thay đổi trong phiên bản 3.2: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'17 now iterates on fp instead of calling >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'50. Đối với mã hiện tại gọi >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'17 với các đối số không có sự lặp lại hỗ trợ, trình tạo sau đây có thể được sử dụng làm trình bao bọc xung quanh đối tượng giống như tệp: >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'8 Thay vì >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'52 sử dụng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'53. configparser.max_interpolation_depth¶MAX_INTERPOLATION_DEPTH¶ Độ sâu tối đa để nội suy đệ quy cho >>> topsecret.getboolean('ForwardX11') False >>> config['bitbucket.org'].getboolean('ForwardX11') True >>> config.getboolean('bitbucket.org', 'Compression') True3 khi tham số thô là sai. Điều này chỉ có liên quan khi phép nội suy mặc định được sử dụng. RawConfigParser Objects¶classConfigParser.RawConfigParser (mặc định = none, Dict_type = dict, allow_no_value = false, *, delimiter = ('=', ':'), bình luận_prefixes = ('#', ';') clank_lines_in_values = true, default_section = configParParser.defaultSect [, nội suy]) ¶ configparser.RawConfigParser(defaults=None, dict_type=dict, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT[, interpolation])¶Biến thể kế thừa của >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9. Nó có nội suy bị vô hiệu hóa theo mặc định và cho phép các tên phần không chuỗi, tên tùy chọn và giá trị thông qua các phương thức >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'56 và >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...80 không an toàn, cũng như xử lý đối số từ khóa legacy >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'58. Đã thay đổi trong phiên bản 3.8: Dict_type mặc định là >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...15, since it now preserves insertion order. Ghi chú Xem xét sử dụng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 thay vào đó kiểm tra các loại giá trị được lưu trữ bên trong. Nếu bạn không muốn nội suy, bạn có thể sử dụng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'61. add_section (phần) ¶(section)¶ Thêm một phần có tên phần vào thể hiện. Nếu một phần theo tên đã cho đã tồn tại, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...83 sẽ được nêu ra. Nếu tên phần mặc định được thông qua, >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...01 sẽ được nâng lên. Loại phần không được kiểm tra cho phép người dùng tạo các phần không có tên. Hành vi này không được hỗ trợ và có thể gây ra lỗi nội bộ. Đặt (phần, tùy chọn, giá trị) ¶(section, option, value)¶Nếu phần đã cho tồn tại, đặt tùy chọn đã cho thành giá trị được chỉ định; Nếu không, tăng >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'38. Mặc dù có thể sử dụng >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...47 (hoặc >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'9 với các tham số thô được đặt thành TRUE) để lưu trữ nội bộ các giá trị không chuỗi, chức năng đầy đủ (bao gồm nội suy và đầu ra vào các tệp) chỉ có thể đạt được bằng cách sử dụng các giá trị chuỗi. Phương thức này cho phép người dùng gán các giá trị không chuỗi cho các khóa bên trong. Hành vi này không được hỗ trợ và sẽ gây ra lỗi khi cố gắng ghi vào tệp hoặc nhận nó ở chế độ không raw. Sử dụng API giao thức ánh xạ không cho phép các bài tập như vậy diễn ra.Use the mapping protocol API which does not allow such assignments to take place. Ngoại lệ haExceptionConfigParser.error¶ configparser.Error¶Lớp cơ sở cho tất cả các ngoại lệ >>> another_config = configparser.ConfigParser() >>> another_config.read('example.ini') ['example.ini'] >>> another_config['topsecret.server.com']['Port'] '50022' >>> another_config.read_string("[topsecret.server.com]\nPort=48484") >>> another_config['topsecret.server.com']['Port'] '48484' >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}}) >>> another_config['topsecret.server.com']['Port'] '21212' >>> another_config['topsecret.server.com']['ForwardX11'] 'no'4 khác. ngoại lệconfigparser.nosectionerror¶configparser.NoSectionError¶ Ngoại lệ được nâng lên khi một phần được chỉ định không được tìm thấy. ExceptionConfigParser.DuplicateSectionErrorror¶ configparser.DuplicateSectionError¶Ngoại lệ được nêu ra nếu >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'68 được gọi với tên của một phần đã có hoặc trong trình phân tích cú pháp nghiêm ngặt khi một phần nếu được tìm thấy nhiều hơn một lần trong một tệp đầu vào, chuỗi hoặc từ điển. Mới trong phiên bản 3.2: Tùy chọn >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'69 and >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'70 attributes and arguments to >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...11 were added. ExceptionConfigParser.DuplicateOptionErrorror¶configparser.DuplicateOptionError¶ Ngoại lệ được nâng lên bởi trình phân tích cú pháp nghiêm ngặt nếu một tùy chọn duy nhất xuất hiện hai lần trong khi đọc từ một tệp, chuỗi hoặc từ điển. Điều này bắt được lỗi chính tả và các lỗi liên quan đến độ nhạy của trường hợp, ví dụ: Một từ điển có thể có hai khóa đại diện cho cùng một khóa cấu hình không nhạy cảm với trường hợp. ExceptionConfigParser.NooptionErrorror¶ configparser.NoOptionError¶Ngoại lệ được nâng lên khi một tùy chọn được chỉ định không được tìm thấy trong phần được chỉ định. ExceptionConfigParser.InterpolationErrorror¶ configparser.InterpolationError¶Lớp cơ sở cho các trường hợp ngoại lệ được nêu khi các vấn đề xảy ra thực hiện nội suy chuỗi. ExceptionConfigParser.InterpolationDepthError¶ configparser.InterpolationDepthError¶Ngoại lệ được nâng lên khi không thể hoàn thành nội suy chuỗi vì số lần lặp vượt quá >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'72. Lớp con của >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'73. ExceptionConfigParser.InterpolationMissingOptionErrorror¶ configparser.InterpolationMissingOptionError¶ Ngoại lệ được nâng lên khi một tùy chọn được tham chiếu từ một giá trị không tồn tại. Lớp con của >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'73. ExceptionConfigParser.InterpolationSyntaxError¶ configparser.InterpolationSyntaxError¶ Ngoại lệ được nêu ra khi văn bản nguồn mà sự thay thế được thực hiện không phù hợp với cú pháp cần thiết. Lớp con của >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'73. Ngoại lệ được nêu ra khi cố gắng phân tích một tệp không có tiêu đề phần. ExceptionConfigParser.parsingerror¶ configparser.ParsingError¶Ngoại lệ được nêu ra khi các lỗi xảy ra khi cố gắng phân tích tệp. Thay đổi trong phiên bản 3.2: Thuộc tính >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'76 attribute and >>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = {'ServerAliveInterval': '45', ... 'Compression': 'yes', ... 'CompressionLevel': '9'} >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...11 argument were renamed to >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: ... print(key) user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'69 for consistency. Chú thích 1(1,2,3,4,5,6,7,8,9,10,11)(1,2,3,4,5,6,7,8,9,10,11)Cấu hình phân tích cú pháp cho phép tùy chỉnh nặng.Nếu bạn quan tâm đến việc thay đổi hành vi được phác thảo bởi tham chiếu chú thích, hãy tham khảo phần hành vi phân tích cú pháp tùy chỉnh. |