Python Classify commands
Question: I am writing a python script to classify ip countries as they are in another
file .. for example .. I have 2 files in the script dir

IPCountries.txt contains :-

    
    
    192.168.1.1   |    US,
    188.100.0.0   |    AU,
    

and the file arrange.txt contains :-

    
    
    0="US,CA,UK,GE,"
    1="AU,EG,"
    

Now the script will read each line in IPCountries.txt file and take the value
after "|" like the value "US" and then match it with the value in file
arrange.txt and write it into a new file called 0.txt .

The problem is that i do not know how to do this but i have used some info to
write the next code but i am stuck in the loop in the end of the code as u can
see here ..

    
    
    import re
    import os
    filepath = 'arrange.txt'
    with open(filepath) as file:
        txt = file.read()
    mapping = re.findall(r'(\d+)="(.*)"', txt)
    
    ip = open("IPCountries.txt",'r')
    for line in ip:
    

Any help with the loop or suggestion how to do it but in the same process and
files ?

Thanks


Answer: You could use something like

    
    
    for line in ip:
        ip, country = [e.strip() for e in line.split("|")]
        country = country[:-1] # Strip off comma at the end
    

I'm not sure what you intend to do with this variables, but the basic
extraction process could look like my example code.

