dimanche 13 décembre 2020

if expression to find if text is in a field for a DictReader key

I think I'm pretty close on this but something with the syntax is missing what I'm trying to do. I'll be really verbose here but it may be very obvious what I'm looking for. I'm using this expression that does work fine when I try it with csv.reader but when I use csv.DictReader, the expression seems to fall apart. I favor using the DictReader because it automagically builds the the Dictionary and row data when it imports the csv file. The file I'm actually working with has about 70 some columns and a couple hundred rows so defining the dictionary manually would complicate things. As long as I use this, I only have to worry that the columns have the same names, the order of rows can change and I don't ever have to actually type them in.

Anyway, I have a string of commands I need to run for each row in a dictionary. In some cases, I want to trigger an additional command to run as well. So I set up this small table where I have 172. and 10. IP space in alternating rows. When I have an IP coming from 172.X.x.x.x space, I want to trigger an extra output line. It could be lots of different IP addresses but triggering on whether it has "172." in there should be significant enough to work for me. I'm trying to find a match of "172" in the first field of the dictionary named "row" but it's not getting a hit the way I have it set up. I shouldn't need to indent since I just want an EXTA line added if expression gets a hit. When I couldn't get it to check the dictionary field value I went and even added a variable that would duplicate what's in the field value just to see if it'd work to scrape that instead. No dice. I thought maybe my syntax was off on how to see what's in the field of the key value but I made one where I just check to see if both key values match, totally got a hit on that. So that just confuses me more.

Here's an example of code I have set up and the output it gets. The question is, how to I get either or of the line 2. if statements in the middle to trigger on fields 1 and 3 in the key since they obviously match? I did a print of the whole list of key values before and after the if statements just to confirm that I can read all of them from that indent level so those coming up at the top of my output simply confirms that the data IS there. I also put a print statement just to make sure that the variable I assigned was working too.

It feels like this is either something simple or this type of expression just doesn't work on this type of dictionary. Anyone aware on this one?

CODE:

`

import csv
pre_config = []


with open('ip_list.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0
    for row in csv_reader:
        print(f'Dictionary Contents: {row["IP_ADDRESS_ZLOC"]} - {row["DATA"]}')
        ip = {row["IP_ADDRESS_ZLOC"]}
        pre_config.append(
        f'1. MAKE TEXT FOR EVERY INSTANCE {row["IP_ADDRESS_ZLOC"]}'
        )
        if "172" in {row["IP_ADDRESS_ZLOC"]} :
            pre_config.append(
            f'  2. TRIGGER WHEN IP {row["IP_ADDRESS_ZLOC"]}'
            )
        if "172" in ip :
            pre_config.append(
            f'  2. TRIGGER IS I HIT VAR {row["IP_ADDRESS_ZLOC"]}'
            )
        if {row["IP_ADDRESS_ZLOC"]} == "172.11.11.11":
            pre_config.append(
            f'  3. IP AND DATA ARE EXACT MATCH'
            )
        if {row["IP_ADDRESS_ZLOC"]} == {row["DATA"]}:
            pre_config.append(
            f'  4. BOTH FIELDS MATCH {row["IP_ADDRESS_ZLOC"]} - {row["DATA"]}'
            )
        print(f'Dictionary Repeated: {row["IP_ADDRESS_ZLOC"]} - {row["DATA"]}')
        print('IP IS: {}'.format(ip))

print('\n')
print("\n".join(pre_config))


RESULTS


Dictionary Contents: 172.11.11.11 - public
Dictionary Repeated: 172.11.11.11 - public
IP IS: {'172.11.11.11'}
Dictionary Contents: 10.10.10.10 - private
Dictionary Repeated: 10.10.10.10 - private
IP IS: {'10.10.10.10'}
Dictionary Contents: 172.1.82.5 - 172.1.82.5
Dictionary Repeated: 172.1.82.5 - 172.1.82.5
IP IS: {'172.1.82.5'}
Dictionary Contents: 10.27.57.55 - private
Dictionary Repeated: 10.27.57.55 - private
IP IS: {'10.27.57.55'}


1. MAKE TEXT FOR EVERY INSTANCE 172.11.11.11
1. MAKE TEXT FOR EVERY INSTANCE 10.10.10.10
1. MAKE TEXT FOR EVERY INSTANCE 172.1.82.5
  4. BOTH FIELDS MATCH 172.1.82.5 - 172.1.82.5
1. MAKE TEXT FOR EVERY INSTANCE 10.27.57.55

Process finished with exit code 0

`

Aucun commentaire:

Enregistrer un commentaire