Loop thru textfile, change 3 variables for every line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Loop thru textfile, change 3 variables for every line (/thread-848.html) Pages:
1
2
|
Loop thru textfile, change 3 variables for every line - herbertioz - Nov-09-2016 Hi, I am working with a script which will read and copy vlan, vlanname and subnet from a textfile to variables/list. The reason is that I am going to use these variables to run some commands with a while loop or something, which runs the command with different vlan,vlanname and subnet each time starting at the top of the textfile. The command is: fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip='10.1.1.1/24', preferred='no', virtual='no') I am going to replace the ip='10.1.1.1/24' with one of the variables. Example textfile (the textfile is much larger): 10 vlan-10 10.1.1.1/24 20 vlan-20 10.1.2.1/24 30 vlan-30 10.1.3.1/24 I hope this is clear for you and hope someone can help me RE: Loop thru textfile, change 3 variables for every line - j.crater - Nov-09-2016 Hi, in a nutshell, you probably want to iterate through all the lines your file. Then you would split the string (line) into 3 substrings (your variables) and store the 3 substrings in 3 variables. with open(data_txt, 'r') as f: #open the file for line in f: #iterate through every line of a text file split_string = line.split() #split the line in a text file into substrings, in this case stored in a list "split_string" vlan= split_string[0] #access first element in "split_string" vlanname= split_string[1] #second element.. subnet = split_string[2] #third element... # here I guess you run your commands with vlan, vlanname and subnetDoes this help? RE: Loop thru textfile, change 3 variables for every line - herbertioz - Nov-09-2016 Perfect, J.crater:) thats nice. I have to learn more Python:) Thanks for help RE: Loop thru textfile, change 3 variables for every line - j.crater - Nov-09-2016 My pleasure! And of course, welcome to the forums =) RE: Loop thru textfile, change 3 variables for every line - herbertioz - Nov-09-2016 Thanks! One more thing thou: I need to change 10.1.1.1/24 with the variable subnet. So the output from subnet changes for every line. fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip='10.1.1.1/24', preferred='no', virtual='no') Is it possible to do this: fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip=subnet, preferred='no', virtual='no') RE: Loop thru textfile, change 3 variables for every line - j.crater - Nov-09-2016 Yes it is possible - once you assigned a value to a variable (in this case subnet) you can pass it as an argument to a function. RE: Loop thru textfile, change 3 variables for every line - herbertioz - Nov-09-2016 Ok, but how can I get the output from the variable "subnet" into the red text in the line under: fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip='10.1.1.1/24', preferred='no', virtual='no') RE: Loop thru textfile, change 3 variables for every line - j.crater - Nov-09-2016 I am not sure I understand what the problem is. Variable doesn't have output, it holds a value (of type integer, string...). So where you write 'subnet', code will behave as if you typed the value stored in 'subnet' variable there. Your 2nd code example from post #5 looked just fine, namely the "ip=subnet" part. At this point, variable subnet holds the value read from a file, for example "10.1.1.1/24". So when it is executed it behaves same as if you wrote ip='10.1.1.1/24'. Just as with: x = 5 y = x + 3 y is now 8. In same way subnet is '10.1.1.1/24' These are so basic programming concepts that frankly make me doubt I understood the real problem. So if I didn't get you right try to explain again, I'll try to help further =) RE: Loop thru textfile, change 3 variables for every line - herbertioz - Nov-10-2016 Ok, maybe its me that is unclear:) subnet = split_string[2] #gets the third element in textfile as substring I will run this command to add the subnet: fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip='10.1.1.1/24', preferred='no', virtual='no') How can I change 10.1.1.1/24 in the command with subnet (subnet = split_string[2])? Is it for example so easy like: fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip='subnet', preferred='no', virtual='no') Forget it my mistake! All is good now:) RE: Loop thru textfile, change 3 variables for every line - Larz60+ - Nov-10-2016 sounds like you're looking for: subnet = split_string[2] #gets the third element in textfile as substring fvSubnet = cobra.model.fv.Subnet(fvBD, name='', descr='', ctrl='', ip=subnet, preferred='no', virtual='no') |