Reputation: 51
Input file Test.ini:
;INTEGRITY_PERIOD=300
INTEGRITY_PERIOD=100
Code:
key = None
value = None
with open('hmi.ini', 'r') as inifile:
for line in inifile:
if line.startswith(';INTEGRITY_PERIOD='):
continue;
if line.startswith('INTEGRITY_PERIOD='):
key, value = line.split("=")
break
if value and value.isdigit():
print(value)
else:
print(300)
The above code is always returning 300. Looks like isdigit() is not working or is there anything wrong in my code ?
Upvotes: 0
Views: 66
Reputation: 1121276
Your lines end in a line separator, which you did not remove. Strip the line to remove whitespace from the start and end:
key, value = line.strip().split("=")
A line separator (\n
) is not a digit:
>>> '100\n'.isdigit()
False
>>> '100'.isdigit()
True
Rather than build your own parser, consider using the configparser
module from the standard library. It fully supports the INI format (including using ;
as a comment).
Upvotes: 5