[Solved] Process each element in a list for each line from a text file [closed]


You need to split each string into each port, then print each one.

And you can use enumerate() to get the line number.

# Example data
port_strings = [
    '443, 9993',
    '443, 3389, 445',
    '443, 22, 3389, 23',
    '3389, 445, 443',
    '443, 3389, 445']

for i, port_string in enumerate(port_strings, 1):
    print('line', i)
    for port in port_string.split(', '):
        print(' print port', port)

Output:

line 1
 print port 443
 print port 9993
line 2
 print port 443
 print port 3389
 print port 445
line 3
 print port 443
 print port 22
 print port 3389
 print port 23
line 4
 print port 3389
 print port 445
 print port 443
line 5
 print port 443
 print port 3389
 print port 445

solved Process each element in a list for each line from a text file [closed]