Home > Net >  How to loop and index through file content in python and assign each line for different variable
How to loop and index through file content in python and assign each line for different variable

Time:08-25

If I have a file.txt And the content looks like this:

   
BEGAN_SIT
s_alis='HTTP_WSD'
xps_entity='HTTP_S_ER'
xlogin_mod='http'
xdest_addr='sft.ftr.net'
xmax_num='99'
xps_pass='pass'
xparam_nm='htp'
#?SITE END

How i I can loop through it and assign each line for different variable

CodePudding user response:

some simple string manipulation and splitting will do what you want. I suggest to read up on str.split(), str.startswith(), general file handling and list handling

with open("filename") as infile:
    for line in infile.read().splitlines():
        if line.startswith("s_alis"):
            s_alis = line.split("'")[1]
        elif line.startswith("xps_entity"):
            xps_entity = line.split("'")[1]
        #etc
  • Related