【发布时间】:2019-05-09 14:01:41
【问题描述】:
我正在尝试创建一个程序,该程序将根据用户的输入从 CSV 中提取某种类型的电缆。有了那根电缆,我将使用它的规格来求解方程。
我正在尝试从每根电缆中制作一个字典,以便提取它并使用它来求解方程会更加顺畅。这是代码。
import csv
import math
with open("origionalconductordata.csv", "r") as file:
#read the file
reader = csv.reader(file)
cable = {}
#creating a cable dictionary
for column in reader:
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:
print "Diameter not found."
find_cable()
else:
print "Unable to find request."
find_cable()
预期结果是代码将为您提供与用户“规格编号”和“尺寸”相匹配的电缆。使用电缆字典中的信息,它将求解方程。我的代码中的实际结果是,当您输入“规格编号”和“尺寸”时,会弹出 else 语句“无法找到请求”。
【问题讨论】:
-
@snakecharmerb 第 10 列
标签: python python-2.7 csv dictionary