There are expressions in python3.

ips = ['10.10.10.98', '10.10.10.99'] for ip in ips: print(ip) 
 10.11.3.98 10.11.3.99 #Вывод ip 

How in turn displays these ip?

For example: to add these ip (in turn) to a text file. Now he adds only the latest ip 10.11.3.99.

  • one
    If it adds only the last one, then it’s written in the code to add only the last one. Show the code that writes to the file. - Enikeyschik
  • How to alternately transfer these ip to the request - Aba

4 answers 4

 ips = ['10.10.10.98', '10.10.10.99'] with open("ips.txt", mode = "w") as file: # открываем файл for ip in ips: # проходим в цикле по списку и пишем в файл file.write(ip + "\n") 
  • If I need to transfer these ip to the request in turn, how to do it?) - Aba
  • For example, you can loop through the list and in turn pass the next element to a function. That is, instead of file.write(ip + "\n") will be some_function(ip) . - Alexshev92

You can even write to write to the file. It has a file parameter, which specifies where print will print lines, and, by default, is sys.stdout . But you can make a conclusion to the file:

 ips = ['10.10.10.98', '10.10.10.99'] with open('ips.txt', 'w') as f: for ip in ips: print(ip, file=f) 

In this case, \n you can not add, because print itself adds it to the end of the line. This can be changed using the end parameter.

  • four
    In general, print in Python is really a magic thing, because you can do it like this: with open('ips.txt', 'w') as f: print(*['10.10.10.98', '10.10.10.99'], sep='\n', file=f) - greg zakharov

You can make a line from the list and write it to a file:

 ips = ['10.10.10.98', '10.10.10.99'] with open("ips.txt", mode="w") as f: file.write("\n".join(ips)) 

    Since here we have a certain “review” of conservation methods, I will allow myself some addition.
    The first. From the above saving options, the slowest option on large files is file.write("\n".join(ips)) . And it will simply “eat” the memory when combined into one giant string variable. The other two are the same in speed and result.
    The second. Usually they save data in a file in order to later read or transfer it to an external user. If the first option is supposed to be “I wrote it down myself then I read it myself”, then saving serialization using the modules pickle, json, shelve is much more efficient.

     import pickle ips = ['10.10.10.98', '10.10.10.99'] with open('ips.pickle', 'wb') as f: pickle.dump(ips, f) 

    which will allow later to "restore" the entire list into a variable, without thinking about how you will parse the lines in ips:

     with open('ips.pickle', 'rb') as f: ips = pickle.load(f) print(ips) 

    json is usually slower, but it retains in almost standard json format, which is more humane - it can be opened and understood - what’s preserved there. There are some more serialization libraries with their own features, such as: jsonpickle , simplejson , demjson . All of them use the json format, and in their own way extend the capabilities of the standard json- module included in the Python distribution.

    The fastest, but dependent on the version of the serialization language, is achieved by the built-in marshal module. The code is almost the same:

     import marshal ips = ['10.10.10.98', '10.10.10.99'] with open('ips.pickle', 'wb') as f: marshal.dump(ips, f) with open('ips.pickle', 'rb') as f: ips = marshal.load(f) print(ips) ['10.10.10.98', '10.10.10.99']