Change list from file - Correct syntax and file format?

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?

+5
source share
3 answers

The contents of the file are not interpreted as Python code. When you read line in f , this is a line; and quotation marks, commas, etc. in your file are only these characters as part of a line.

If you want to create some other data structure from a string, you need to parse it. The program does not know that you want to turn the string "'abc.com', 'abc'" into a tuple ('abc.com', 'abc') if you did not specify it.

This is the point at which the question becomes "too broad."

If you control the contents of a file, you can simplify the data format to make it simpler. For example, if you only have abc.com abc in the line of the file, so that your line ends with 'abc.com abc' , then you can simply .split() that; this assumes that you do not need to represent spaces inside any of the two elements. Instead, you can split into another character (e.g. a comma, in your case) ( .split(',') ). If you need a general-purpose hammer, you may need to look into JSON. There is also ast.literal_eval that can be used to process text as simple Python literals - in this case you will need lines of this file that will also include parentheses.

+1
source

If you are ready to release quotes in your file and rewrite it as

 abc.com, abc def.com, def hji.com, hji 

downloadable code can be reduced to one insert using the fact that the files are iterable

 with open('servers.list') as f: servers = [tuple(line.split(', ')) for line in f] 

Remember that using a file as an iterator already disables newlines.

You can resolve arbitrary spaces by doing something like

 servers = [tuple(word.strip() for word in line.split(',')) for line in f] 

It may be easier to use something like a regular expression to parse the original format. You can use an expression that captures parts of the string that are interesting and relevant to you, but discards the rest:

 import re pattern = re.compile('\'(.+)\',\\s*\'(.+)\'') 

Then you can extract the names from the mapped groups

 with open('servers.list') as f: servers = [pattern.fullmatch(line).groups() for line in f] 

This is just a trivial example. You can make it as complex as you want your real file format.

+1
source

Try the following:

 serverlist = [] with open('/server.list', 'r') as f: for line in f: serverlist.append(tuple(line.rstrip('\n').split(','))) 

Explanation

  • You need an explicit for loop to cycle through each line as expected.
  • You need list.append for each line added to your list.
  • You need to use split(',') to separate by comma.
  • Convert to tuple , as this is your desired result.

List comprehension method

The for loop can be compressed as shown below:

 with open('/server.list', 'r') as f: serverlist = [tuple(line.rstrip('\n').split(',')) for line in f] 
0
source

Source: https://habr.com/ru/post/1275945/


All Articles