1

This is a part of a large csv file which I have:

"66.35.223.128","66.35.223.143","1109647232","1109647247","AU","Australia"
"66.35.223.144","66.35.227.191","1109647248","1109648319","US","United States"
"66.35.227.192","66.35.227.207","1109648320","1109648335","JP","Japan"
"66.35.227.208","66.35.230.31","1109648336","1109648927","US","United States"
"66.35.230.32","66.35.230.47","1109648928","1109648943","AU","Australia"
"66.35.230.48","66.35.236.207","1109648944","1109650639","US","United States"
"66.35.236.208","66.35.236.223","1109650640","1109650655","AU","Australia"
"66.35.236.224","66.36.127.255","1109650656","1109688319","US","United States"

The first two columns are a range of IP addresses. I have an IP address 66.35.250.168 I need to search the csv file to see in which range it lies, and print out the corresponding country name.

Since the first two numbers (66,35) are the same, I intend to search for the line containing this. I can search a complete string(66.35.205.88) by doing this:

import csv
with open('GeoIPCountryWhois.csv', mode='r') as f:
    reader = csv.reader(f)
    for row in reader:
        if row[0] in ['66.35.205.88']:
            print row

If I search for 66.35, I don't get any result . Can you please tell me a way in which I can search for a part of the string ('66.35' here) ? Also, can you tell me how I can find the exact line number in which I find the string?

Thanks in advance.

3
  • Getting the line number is relatively easy. Try changing the fourth line to for line_number, row in enumerate(reader): Commented Aug 4, 2011 at 19:25
  • Your entire approach to the problem is lacking. What you want to do is parse the entire CSV file to create a data structure, and then work with the data structure. "Line numbers" will be implicit from indices into the list of row-structures. You can use a namedtuple to represent each row. Commented Aug 4, 2011 at 21:24
  • Yes, that is exactly what I want to do. I wanted the line number just for reference because I had a really large file. Commented Aug 4, 2011 at 21:48

3 Answers 3

5
import csv
with open('GeoIPCountryWhois.csv', mode='r') as f:
    reader = csv.reader(f)
    for num, row in enumerate(reader):
        if '66.35' in row[0]:
            print num, row

Keep in mind this can give you false positives if '66.35' appears at other locations in the address or elsewhere in the line.

Edit: Here is a version that can actually check if it's in the right range.

def numeric_ip(ip):
    return [int(x) for x in ip.split('.')]

desired_ip = numeric_ip('66.35.205.88')
with open('GeoIPCountryWhois.csv', mode='r') as f:
    for num, row in enumerate(csv.reader(f)):
        if numeric_ip(row[0]) <= desired_ip <= numeric_ip(row[1]):
            print num, row
Sign up to request clarification or add additional context in comments.

1 Comment

I just had to change the '<' to '<=' without which I wasn't getting a result. Thanks a lot! That worked really well.
1

There is no reason in shouldn't work.

Make sure you switch the order

if '66.35' in row[0]:
    print row

2 Comments

No... that's testing if the list is in row[0]. You need to test if the string is in row[0].
Yes, row[0] in ['66.35'] works. By switching you mean to search for the list in the string. I don't get any output if i search for '66.35'
0

You can use standard boolean tests with strings to check if the ip you're looking for is in the range:

import csv

desired_ip = "66.35.232.56"
desired_ip_n = [str(n) for n in desired_ip.split(".")
with open('GeoIPCountryWhois.csv', mode='r') as f:
    reader = csv.reader(f)
    row_num = 1
    for row in reader:
        ip_start_n = [str(n) for n in row[0].split(".")]
        ip_end_n = [str(n) for n in row[1].split(".")]
        if desired_ip_n >= ip_start_n and desired_ip <= ip_end_n:
            print row
            print row_num
        row_num += 1

2 Comments

I don't think so... it's not numeric ordering so 200 will sort less than 30.
yep you're right, I just caught that. But I think you can do a split(".") on the desired ip and the ips defining the range and then compare the resulting lists...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.