【问题标题】:Extracting Information From a CSV从 CSV 中提取信息
【发布时间】: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


【解决方案1】:

你有一个字典,cable,它的键是电缆规范,每个键的值是一个对应于规范的数据字典。

如果cable 具有匹配spec 和size 的键,则表达式spec and size in cable 将计算为True。我认为您真的想检查spec 的 value 是否具有等于size 的“Conductor 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."

最后,如果有很多不同的电缆(比如 1000 根),遍历所有字典以找到匹配的直径会很慢。请考虑将数据加载到 sqlite 之类的数据库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多