Python - read from file skip lines starts with # -


try read file , make dictionary lines, skippipng lines starts # symbol

file example:

param1=val1 # here comment 

my function:

def readfromfile(name):     config = {}     open(name, "r") f:                  line in f.readlines():             li=line.lstrip()             if not li.startswith("#"):                 config[line.split('=')[0]] = line.split('=')[1].strip()     return config 

i list index out of range error

but! if try skip lines starts with, example, symbol "h" - function works well...

try with:

def readfromfile(name):     config = {}     open(name, "r") f:                  line in f.readlines():             li = line.lstrip()             if not li.startswith("#") , '=' in li:                 key, value = line.split('=', 1)                 config[key] = value.strip()     return config 

you maybe have blank line breaks split()


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -