Python Forum
Loop thru textfile, change 3 variables for every line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop thru textfile, change 3 variables for every line
#1
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
Reply
#2
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 subnet
Does this help?
Reply
#3
Perfect, J.crater:) thats nice. I have to learn more Python:) Thanks for help
Reply
#4
My pleasure! And of course, welcome to the forums =)
Reply
#5
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')
Reply
#6
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.
Reply
#7
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')
Reply
#8
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 =)
Reply
#9
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:)
Reply
#10
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  textfile to customtkinter combobox janeik 6 1,722 Aug-31-2023, 02:50 AM
Last Post: deanhystad
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,657 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,481 Jul-27-2022, 08:50 PM
Last Post: rob101
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,477 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Skipping line in text without Restarting Loop IdMineThat 4 1,477 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  How to prevent python from going to new line in for loop? idknuttin 3 4,921 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  How to make for loop display on 1 Line Extra 3 1,426 Jan-12-2022, 09:29 PM
Last Post: Extra
  how to use 3 variables python loop evilcode1 2 1,666 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  Change the varaible in For Loop quest 2 1,715 Aug-13-2021, 03:35 PM
Last Post: deanhystad
  Change variable value during a while loop? penahuse 2 4,050 Nov-15-2020, 11:53 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020