Python: Check one element in csv, use another to remove from second file

By : MHibbin
Source: Stackoverflow.com
Question!

I am trying to get a script working, where it will check the existance of an IP in a lookup csv file, and then if it exists take the third element and remove that third element from another (second) file. Here is a extract of what I have:

for line in fileinput.input(hostsURLFileLoc,inplace =1):
        elements = open(hostsLookFileLoc, 'r').read().split(".").split("\n")
        first = elements[0].strip()
        third = elements[2].strip()
        if first == hostIP:
                if line != third:
                        print line.strip()

This obviously doesn't work, I have tried playing with a few options, but here is my latest (crazy) attempt.

I think the problem is that there are two input files open at once.

Any thoughts welcome,

Cheers

By : MHibbin


Answers

In test file test.csv (note there is an IP address in there):

'aajkwehfawe;fh192.168.0.1awefawrgaer'

(I am pretty much ignoring that it is CSV for now. I am just going to use regex matches.)

# Get the file data
with open('test.csv', 'r') as f:
    data = f.read()

# Look for the IP:
find_ip = '192.168.0.1'
import re
m = re.search('[^0-9]({})[^0-9]'.format(find_ip), data)
if m: # found!
    # this is weird, because you already know the value in find_ip, but anyway...
    ip = m.group(1).split('.')
    print('Third ip = ' + ip[2])
else:
    print('Did not find a match for {}'.format(find_ip))

I do not understand the second part of your question, i.e. removing the third value from a second file. Are there numbers listed line by line, and you want to find the line that contains this number above and delete the line? If yes:

# Make a new list of lines that omits the matched one
new_lines=[]
for line in open('iplist.txt','r'):
    if line.strip()!=ip[2]: # skip the matched line
        new_lines.append(line)

# Replace the file with the new list of lines
with open('iplist.txt', 'w') as f:
    f.write('\n'.join(new_lines))
By : cjrh


If, once you have found values in the first file that need to be removed in the second file, I suggest something like this pseudocode:

Load first file into memory
Search string representing first file for matches using a regular expression
    (in python, check for re.find(regex, string), where regex = re.compile("[0-9]{3}\\.[0-9]{3}\\.[0-9]\\.[0-9]"), I am not entirely certain that you need the double backslash here, try with and without)
Build up a list of all matches
Exit first file

Load second file into memory
Search string representing second file for the start index and end index of each match
For each match, use the expression string = string[:start_of_match] + string[end_of_match:]
Re-write the string representing the second (now trimmed) file to the second file

Essentially whenever you find a match, redefine the string to be the slices on either side of it, excluding it from the new string assignment. Then rewrite your string to a file.



This video can help you solving your question :)
By: admin