简体   繁体   English

根据python中的用户输入创建多个文件

[英]Create multiple files based on user input in python

i am new to python and I've written a code which create configuration files for my application. 我是python的新手,我写了一个代码,为我的应用程序创建配置文件。 I've created the code which works for 2 IP's but it may happen that user may input more IP's and for each increase in Ip the config file will be changed. 我已经创建了适用于2个IP的代码,但是可能会发生用户可能输入更多IP的情况,并且IP的每次增加都会更改配置文件。 There are authentication servers and they can be either 1 or 2 only. 有身份验证服务器,它们只能是1或2。

I am passing input to python code by a file name "inputfile", below is how it look like: 我正在通过文件名“ inputfile”将输入传递给python代码,如下所示:

EnterIp_list: ip_1 ip_2
authentication_server: as_1 as_2

Below is how the final configuration files are created: 以下是最终配置文件的创建方式:

configfile1:                  configfile2:
App_ip: ip_1                  App_ip: ip_2
app_number: 1                 app_number: 2
authen_server: as_1           authen_server: as_2

Below is how python3 code looks: 以下是python3代码的外观:

def createconfig(filename, app_ip, app_number, authen_server)
     with open(filename, 'w') as inf:
          inf.write("App_ip=" + app_ip + "\n")
          inf.write("app_numbber=" + app_number)
          inf.write("authen_server="+ authen_server)


with open("inputfile") as f:
     for line in f:
       if EnterIP_list in line:
          a= line.split("=")
          b = a[1].split()
       if authentiation_server in line:
          c= line.split("=")
          d=c[1].split()

createconfig(configfile1, b[0], 1, d[0])
createconfig(configfile2, b[1], 2, d[1])

Users has freedom to input as many IP's as they wish for. 用户可以自由输入所需数量的IP。 Can someone please suggest what need to be done to make code more generic and robust so that it will work for any number of input ip's ??? 有人可以提出一些建议,使代码更通用,更健壮,以便对任何数量的输入ip都适用吗? also value for app_number increases with each new ip added. 随着每个新IP的添加,app_number的值也会增加。

There will always be two authentication server and they go in round robin eg the third app ip will be associated to "as_1" again. 始终会有两个身份验证服务器,它们轮流访问,例如,第三个应用程序ip将再次与“ as_1”关联。

You just need to iterate over your ip list in b, be aware that your current code only works for the last line of your "inputfile". 您只需要遍历b中的ip列表,请注意,当前代码仅适用于“ inputfile”的最后一行。 As long as there is only one line, thats ok. 只要只有一行,那就可以了。

with open("inputfile") as f:
     for line in f:
        a= line.split("=")
        b = a[1].split()

app_count = 1
for ip in b:
    createconfig("configfile%s" % app_count , ip, app_count)
    app_count += 1

Edit: Solution updated regarding your code change. 编辑:解决方案已更新有关您的代码更改。

with open("inputfile") as f:
     for line in f:
       if EnterIP_list in line:
          ips = line.split("=")[1].split()
       if authentiation_server in line:
          auth_servers = line.split("=")[1].split()

app_count = 1
for ip, auth_server in zip(ips, auth_servers):
    createconfig("configfile%s" % app_count , ip, app_count, auth_server)
    app_count += 1

A not so great way of doing it without modifying so much of your code would be to remove the last two createconfig() calls and instead do it in a loop once you have b as follows: 在不修改大量代码的情况下,这样做的一种不太好的方法是删除最后两个createconfig()调用,而一旦拥有b,则按如下所示循环执行:

with open("inputfile") as f:
 for line in f:
    a= line.split("=")
    b = a[1].split()

for app_number in b:
    createconfig("configfile{}".format(app_number), b[app_number], app_number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM