Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: best way to join multiple lists into multiple new files

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading