1

I'm trying to get Python to concatenate multiple lists in multiple new files. At the end of each "go" I would write the output into a file. I would need this to generate multiple configuration files where the user/pass/mac would be changing, while most other stuff would stay the same.

I tried to use the for loop to perform this, but I can not get it to work. In the output file there would be some rows that would be the same across all the files and some rows where the data would be provided from two separate lists.

Output should look something like this (the file in the end will consist of 100+ rows):

Initial data:

user=["user1","user2","user3"]
password=["pass1","pass2","pass3"]
mac=["mac1","mac2","mac3"]

Resulting files:

mac1.cfg:

#configuration
username= user1
password= pass1
server= 1.1.1.1
option1= enable

mac2.cfg:

#configuration
username= user2
password= pass2
server= 1.1.1.1
option1= enable

and so on...

I’m sure that this can be done in many other (easier) ways, but I would like to solve the problem by using python. So far (in my limited python skill) I first tried to get out a single batch of data and this works. When I try to use the for loop to achieve the same thing for all the “sets” I get stuck.

Single batch code looks like this:

user=["user1","user2","user3"]
password=["pass1","pass2","pass3"]
mac=["mac1","mac2","mac3"]
file1 = open(mac[0]+".cfg", "x")
line1=("#configuration")
line2=("username= "+user[0])
line3=("password= "+password[0])
line4=("server= 1.1.1.1")
line5=("option1= enable")
text=line1+"\n"+line2+"\n"+line3+"\n"+line4+"\n"+line5
file1.writelines(text)
file1.close()

If I try to multiplicate the logic with a for loop I get stuck... can you please help me out?

Demions
  • 27
  • 3
  • You can implement this with a `for x in range`: `for i in range len(mac): fname = mac[i] username= user[i] passwd = password[i] content= f'username={username}\npassword={passwd}\nserver={sever}\noption1={option1}'` – vinalti Jan 23 '22 at 18:08
  • So, the first time through the loop, you want to have `mac[0]`, `user[0]`, `password[0]`, etc., and then you know what to do with all of them to create the appropriate file, yes? And then next time through it should use `mac[1]`, `user[1]`, `password[1]` etc.? And so on? So, you are trying to *iterate* (the word for the thing you do to a list when you use a `for` loop) through all of those lists *in parallel* (the term for using multiple things simultaneously, by analogy from electrical circuits). When you know the words, it becomes much easier to search. Please see the linked duplicate. – Karl Knechtel Jan 23 '22 at 18:08

1 Answers1

1

Something like this:

user = ["user1", "user2", "user3"]
password = ["pass1", "pass2", "pass3"]
mac = ["mac1", "mac2", "mac3"]

for i,u in enumerate(user):
    file1 = open(mac[i]+".cfg", "w")
    line1 = "#configuration"
    line2 = "username= " + user[i]
    line3 = "password= " + password[i]
    line4 = "server= 1.1.1.1"
    line5 = "option1= enable"
    text = "\n".join([line1,line2,line3,line4,line5])
    file1.write(text)
    file1.close()

https://www.w3schools.com/python/ref_func_enumerate.asp

https://realpython.com/python-enumerate/

A little more 'pythonic' way with zip():

users = ["user1", "user2", "user3"]
passwords = ["pass1", "pass2", "pass3"]
macs = ["mac1", "mac2", "mac3"]

for user, pas, mac in zip(users, passwords, macs):
    file1 = open(mac + ".cfg", "w")
    line1 = "#configuration"
    line2 = "username= " + user
    line3 = "password= " + pas
    line4 = "server= 1.1.1.1"
    line5 = "option1= enable"
    text = "\n".join([line1,line2,line3,line4,line5])
    file1.write(text)
    file1.close()
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23