Skip to content Skip to sidebar Skip to footer

Compare A X.split()[field] With Some Values

I have the following code: with open('/home/adiel/log', 'r') as f: for line in [[x.split()[3], x.split()[4], x.split()[5], x.split()[6]] for x in f]: print(line) It

Solution 1:

You can create a generator with the splits then print the filtered values:

withopen('/home/adiel/log', 'r') as f:
    iter_lines = (x.split() for x in f)
    for line in (x[3:7] for x in iter_lines if x[5] in {'443', '65535'}):
        print(line)

Or use a for loop to split() the lines and print if the condition is met:

withopen('/home/adiel/log', 'r') as f:
    for line in f:
        line = line.split()
        if line[5] in {'443', '65535'}:
            print(line[3:7])

Post a Comment for "Compare A X.split()[field] With Some Values"