Python Forum
Define a range, return all numbers of range that are NOT in csv data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Define a range, return all numbers of range that are NOT in csv data
#1
Hi There,

I have a CSV file that contains the following data

"LineURI";"Type";"Number";"Extension"
"tel:+61383997000;ext=97000";"csuser";"+61383997000";"97000"
"tel:+61383997001;ext=97001";"csuser";"+61383997001";"97001"
"tel:+61383997003;ext=97003";"csuser";"+61383997003";"97003"
"tel:+61383997004;ext=97004";"csuser";"+61383997004";"97004"
"tel:+61383997099;ext=97099";"csmeetingroom";"+61383997099";"97099"

I would like to query the "Number" field in the CSV data via a range
(+61383997000,+61383997099)
and return all numbers within the range that are not included in the Number field in csv file.
Reply
#2
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I Suppose I d0nt know where to start with this kind of code.

I have built a part of script that asks the user to enter a number then return the row of that number within the csv file. I would like to put in the option for input to either search for a particular number and return the informaiton, or search for a range and return all numbers that are not included.

the code I have for the searching of a single number is

import csv
import codecs

# Open using dictReader
with open('AssignedLineURI.csv', 'rt') as AssignedLineURI:
    # Open file in UTF Codec
    csv_data = csv.DictReader(codecs.open('AssignedLineURI.csv', 'rU', 'utf-16', ),delimiter =';')

    # Input
    # Get Number in 10 digit format from input
    csenteredNumber = input("Enter User's Line Number in E164 Format: ")
    csLineURI = "tel:" + csenteredNumber + ";ext=" + csenteredNumber[-5:]
    # print(csLineURI)
    
    # Match Line URI to User
    for row in csv_data:
        if row['LineURI'] == csLineURI:
            print (row['DisplayName'] + " - " + row['SipAddress'] + " - " + row['DID'])
This will prompt input for a telephone number, search the csv, and return the fields specified in the row.

Im assuming to being my search of number not contained in the csv file, I would need to define a range, create a loop to search each number in the range and return if NOT in matching csv.
Reply
#4
If you are doing a range, a range has a minimum and maximum. Can't you just check if the numbers are less than the minimum or greater than the maximum?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks Ichabod.

I am trying your suggestions. What do you think of my approach this way.
Im starting simple and just going for a user input and performing a comparison on variable numbers.

But I feel what am I doing is not going to scale well. The aim is to be able to add and remove ranges easily.

# Define Site Ranges

_range_549stkilda_start = (61392697000)
_range_549stkilda_end = (61392698000)
_range_549stKilda_name = ("549 StKilda Rd")



# User Input
userinput = input("Enter the Number Yo")
number = int(userinput)
print ("type fo number", type(number))
# number2 = +61392697352

# def find_range(n):
if _range_549stkilda_start <= number <= _range_549stkilda_end:
    print("Number is a " + _range_549stKilda_name + " Number")
else:
    print("The number is outside the range")
Reply
#6
If you want to add and remove ranges easily, use a common data structure for your range, like a dictionary:

_range_549stkilda = {'start': 61392697000, 'end': 61392698000, name: "549 StKilda Rd"}

# User Input
userinput = input("Enter the Number Yo")
number = int(userinput)
 
def find_range(n, num_range):
    if num_range['start'] <= number <= num_range['end']:
        print("Number is a " + num_range['name'] + " Number")
    else:
        print("The number is outside the range")

find_range(number, _range_549stkilda)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
You are brilliant.

So now how do I add another rnage to the top.

_range_549stkilda = {'start': 61392697000, 'end': 61392698000, 'name': "549 StKilda Rd"}
_range_rhodes = {'start': 61298166000, 'end': 612981669999, 'name': "Rhodes"}
 
# User Input
userinput = input("Enter the Number Yo")
number = int(userinput)
  
def find_range(n, num_range):
    if num_range['start'] <= number <= num_range['end']:
        print("Number is a " + num_range['name'] + " Number")
    else:
        print("The number is outside the range")
 
find_range(number, _range_549stkilda)
Reply
#8
Just like you did. Now you can change which range you send to the function when you call it on line 14.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Ok. SO lets say I have the following and I want to return the range that the number falls in. How do I define the find range function to search in each range?

_range_549stkilda = {'start': 61392697000, 'end': 61392698000, 'name': "549 StKilda Rd"}
_range_rhodes = {'start': 61298166000, 'end': 612981669999, 'name': "Rhodes"}
_range_mulgrave = {'start': 61385841000, 'end': 61385841199, 'name': "Mulrgave"}
  
# User Input
userinput = input("Enter the Number Yo")
number = int(userinput)
   
def find_range(n, num_range):
    if num_range['start'] <= number <= num_range['end']:
        print("Number is a " + num_range['name'] + " Number")
    else:
        print("The number is outside the range")
  
find_range(number, _range_549stkilda)
Reply
#10
Make an in_range function. It would be like find_range, but it would return True or False. Note that you can do this in one line if you do it right.

Next, put all of the ranges into a list, or a dictionary with the names as keys. That allows you to loop through the ranges, checking each one in turn with in_range. When you get True, break out of the loop and you've got the matching range.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,637 Jan-05-2024, 08:30 PM
Last Post: sgrey
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,438 May-14-2023, 04:29 PM
Last Post: Winfried
Thumbs Down I hate "List index out of range" Melen 20 3,306 May-14-2023, 06:43 AM
Last Post: deanhystad
  How to check multiple columns value within range SamLiu 2 1,142 Mar-13-2023, 09:32 AM
Last Post: SamLiu
  Expand the range of a NumPy array? PythonNPC 0 746 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Copying files if the name is in range tester_V 9 1,524 Nov-24-2022, 12:21 AM
Last Post: tester_V
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,317 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Solving an equation by brute force within a range alexfrol86 3 2,774 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  IndexError: list index out of range dolac 4 1,900 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,333 Jun-26-2022, 09:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020