从CSV提取信息

时间:2019-05-09 14:01:41

标签: python python-2.7 csv dictionary

我正在尝试创建一个程序,该程序将根据用户的输入从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语句“无法找到请求”。

1 个答案:

答案 0 :(得分:1)

您有一个字典cable,它以电缆规格作为键,每个键的值是对应于规格的数据字典。

如果spec and size in cable具有与Truecable匹配的,则表达式spec的计算结果为size。我认为您真的想检查spec的“导体尺寸”值是否等于size

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

在未提供规格的情况下,您似乎正在尝试查找具有给定“直径尺寸”和正确的“导体尺寸”的电缆。由于cable的键不是直径大小,因此next in cable永远不会为真,除非巧合。您需要检查cable中的每个值以查看其是否符合您的要求。

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()

处理重复请求输入的通常方法是使用while循环,而不是递归调用find_cable。因此,该函数最终看起来像这样的未经测试的代码:

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
                print "Diameter not found."

        else:
            print "Unable to find request."

最后,如果有许多不同的电缆(例如1000s),循环遍历所有字典以找到匹配的直径将很慢。考虑将数据加载到类似sqlite的数据库中。