Simple INI File Parser in Python
INI files are a common configuration file format used across various applications. This challenge asks you to implement a basic INI file parser in Python, enabling you to read and extract data from these files. This is a valuable skill for interacting with configuration settings and data stored in this widely used format.
Problem Description
You are tasked with creating a Python function that parses a simple INI file and returns a dictionary representing the file's contents. The INI file will consist of sections (denoted by [section_name]) and key-value pairs within each section. The function should handle multiple sections, and key-value pairs separated by an equals sign (=). Whitespace around the equals sign should be ignored. Comments, denoted by a semicolon (;), should be ignored. Blank lines should also be ignored.
Key Requirements:
- Section Handling: The parser must correctly identify and separate sections.
- Key-Value Extraction: It must extract key-value pairs from each section.
- Whitespace Handling: Whitespace around the equals sign should be trimmed.
- Comment Handling: Lines starting with a semicolon (
;) should be ignored. - Blank Line Handling: Blank lines should be ignored.
- Data Structure: The parsed data should be returned as a dictionary where keys are section names and values are dictionaries of key-value pairs within that section.
Expected Behavior:
The function should take the INI file content as a single string and return a dictionary. If the input string is empty or contains only comments/blank lines, the function should return an empty dictionary.
Edge Cases to Consider:
- Empty INI file.
- INI file with only comments.
- INI file with blank lines.
- Sections with no key-value pairs.
- Key-value pairs with no values (e.g.,
key=). - Multiple spaces around the equals sign.
- Comments at the end of lines.
Examples
Example 1:
Input: """
[section1]
key1 = value1
key2 = value2 ; comment
key3=value3
[section2]
key4 = value4
"""
Output: {'section1': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}, 'section2': {'key4': 'value4'}}
Explanation: The input string is parsed into two sections, 'section1' and 'section2'. Each section contains key-value pairs, with whitespace around the equals sign trimmed and comments ignored.
Example 2:
Input: """
; This is a comment
[section1]
key1 = value1 ; another comment
"""
Output: {'section1': {'key1': 'value1'}}
Explanation: The comment at the beginning is ignored. Whitespace around the equals sign is trimmed.
Example 3:
Input: """
[section1]
[section2]
"""
Output: {'section1': {}, 'section2': {}}
Explanation: Sections exist but contain no key-value pairs.
Example 4:
Input: ""
Output: {}
Explanation: Empty input results in an empty dictionary.
Constraints
- Input Size: The input INI file content string will be no longer than 10,000 characters.
- Section Names: Section names will consist of alphanumeric characters and underscores, enclosed in square brackets.
- Key-Value Pairs: Keys and values will consist of alphanumeric characters, underscores, and hyphens.
- Performance: The parsing should complete within 0.5 seconds for the maximum input size.
- No external libraries: You are not allowed to use external libraries like
configparser.
Notes
- Consider using string manipulation techniques like
split()andstrip()to parse the INI file content. - Regular expressions could be used, but are not strictly necessary for this simple implementation.
- Think about how to handle different line endings (e.g.,
\n,\r\n). The provided examples use\n. - Focus on clarity and readability in your code. Good variable names and comments are encouraged.
- The order of key-value pairs within a section is not important.
- The values are treated as strings. No type conversion is required.