I currently have a list encoded in my Python code. As it continues to expand, I would like to make it more dynamic by reading the list from a file. I have read many articles on how to do this, but in practice I cannot get this to work. So, firstly, here is an example of an existing hard-copy list:
serverlist = [] serverlist.append(("abc.com", "abc")) serverlist.append(("def.com", "def")) serverlist.append(("hji.com", "hji"))
When I enter the "print server list" command, the result is shown below, and my list works fine when I access it:
[('abc.com', 'abc'), ('def.com', 'def'), ('hji.com', 'hji')]
Now I have replaced the above code with the following:
serverlist = [] with open('/server.list', 'r') as f: serverlist = [line.rstrip('\n') for line in f]
With the contents of server.list:
'abc.com', 'abc' 'def.com', 'def' 'hji.com', 'hji'
When I enter the print serverlist command print serverlist , the output will be shown below:
["'abc.com', 'abc'", "'def.com', 'def'", "'hji.com', 'hji'"]
And the list does not work correctly. So what am I doing wrong? Am I reading a file incorrectly or formatting a file incorrectly? Or something else?