Parsing a Space-Delimited Configuration File in Python - implementing Trailing Delimiters
Could someone explain I'm currently working on a Python script to parse a simple space-delimited configuration file. The file can optionally include comments that start with a `#`. However, I'm running into issues when some lines have trailing spaces, which causes my parsing logic to behave unexpectedly. Specifically, I'm using Python 3.10 and the following approach: ```python config_lines = [] with open('config.txt', 'r') as file: for line in file: line = line.strip() # Remove leading and trailing whitespace if line and not line.startswith('#'): config_lines.append(line.split()) # Split by whitespace ``` This mostly works, but when I have a line like `key1 value1 # This is a comment`, the output ends up being `[['key1', 'value1', '']]` due to the trailing spaces before the comment. I tried adding a second `strip()` on the split result, but it seems awkward and not very efficient. Additionally, I'm unsure how to handle lines that might only contain comments or empty spaces. I've thought about using regular expressions, but I'm concerned about performance optimization and code readability. The current list `config_lines` can also get confusing if it contains empty strings. Has anyone encountered this scenario before? What would be a best practice for handling this type of input while keeping the code clean and efficient? Any ideas what could be causing this? The project is a microservice built with Python. Any ideas how to fix this?