# Extracting Information From a CSV

I am trying to create a program that will extract a certain type of cable from a CSV based upon the user's inputs. With that cable I will use the specs of it to solve equations.

I am trying to make a dictionary out of each cable so that extracting it and using it to solve equations will be a lot smoother. Here's the code.

``````import csv
import math

with open("origionalconductordata.csv", "r") as file:

cable = {}
#creating a cable dictionary
cable[column[1]] = {'Stock Number':column[2], 'Overall Diameter':column[49],
'Diameter Over Conductor':column[40], 'Conductor Size': column[10]}

#Finding out what cable the user wants
def find_cable():
spec = raw_input("Type the cable Spec Number below.  If you do not have a Spec Number, type 'no'.\n>")
size = raw_input("Type the size of the cable.\n>")

if spec and size in cable:
print cable[spec][size]
find_equation()

elif spec == "no":
next = raw_input("Type the Overall Diameter of the cable.\n>")

if next in cable:
print cable[next][size]
find_equation()

else:
find_cable()

else:
print "Unable to find request."
find_cable()
``````

The expected result is that the code will give you a cable that matches the users 'Spec Number' and 'Size'. With the information that's in the cable's dictionary, it will solve equations. The actual result in my code is when you type in a 'Spec Number' and 'Size' the else statement "Unable to find request" pops up.

On

You have a dictionary, `cable`, which has cable specifications as its keys, and the value for each key is a dictionary of data corresponding to the specification.

The expression `spec and size in cable` will evaluate to `True` if `cable` has keys which match `spec` and `size`. I think you really want to check whether the value for `spec` has a value for "Conductor Size" equal to `size`:

``````if spec in cable and cable[spec]['Conductor Size'] == size:
find_equation()
``````

In the case where no spec is provided, you seem to be trying to find a cable with a given "Diameter Size" and the correct "Conductor Size". Since the keys of `cable` are not diameter sizes, `next in cable` will never be true, except by coincidence. You'll need to check each value in `cable` to see if it matches your requirements.

``````elif spec == "no":
# Use meaningful variable names to show your intent.
diameter_size = raw_input("Type the Overall Diameter of the cable.\n>")

# Search for matches.
for k, v in cable.items():
if (v['Diameter Size'] == diameter_size
and v['Conductor Size'] == size):
find_equation()

if next in cable:
print cable[next][size]
find_equation()
``````

The usual way to handle repeatedly asking for input like this is to use a `while` loop, rather than having `find_cable` call itself recursively. So the function would end up looking like this untested code:

``````def find_cable():
while True:
spec = raw_input("Type the cable Spec Number below.  If you do not have a Spec Number, type 'no'; type 'q' to quit.\n>")
conductor_size = raw_input("Type the size of the cable.\n>")

if spec in 'Qq':
print('Bye!')
break

if (spec in cable
and cable[spec]['Conductor Size'] == conductor_size):
find_equation()

elif spec == "no":
diameter_size = raw_input("Type the Overall Diameter of the cable.\n>")

# Search for matches.
for k, v in cable.items():
if (v['Diameter Size'] == diameter_size
and v['Conductor Size'] == size):
find_equation()
# Break out of the for loop once we find a match.
break
else:
# For loops have an else clause that is triggered if the code
# doesn't break out of the loop