You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python CSV线缆数据提取程序匹配失败问题求助

Got it, let's figure out why your code is hitting that "Unable to find request." message and fix it up step by step!

1. The Core Problem: Mismatched Dictionary Structure vs. Your Lookup Logic

Right now, your cable dictionary is structured like this:

{
  "spec_number_1": {"Stock Number": "xxx", "Overall Diameter": "yyy", ...},
  "spec_number_2": {"Stock Number": "aaa", "Overall Diameter": "bbb", ...},
  ...
}

Each spec number maps directly to a single cable's properties. But here's the issue:

  • When you check if spec and size in cable:, you're checking if size is a top-level key in cable—which it never will be, since top-level keys are only spec numbers.
  • Worse, if your CSV has multiple cables with the same spec number, your loop overwrites the existing entry every time, so you'll only ever keep the last one.

2. Fix #1: Rebuild the Dictionary for Proper Lookup

We need a nested dictionary where we can look up by both spec number and conductor size. Here's how to adjust the CSV reading code:

import csv
import math

# Now structured as {spec_number: {conductor_size: full_cable_properties}}
cable = {}

with open("origionalconductordata.csv", "r") as file:
    reader = csv.reader(file)
    # Important: If your CSV has a header row, add this line to skip it:
    # next(reader)
    
    for row in reader:
        spec_num = row[1]
        conductor_size = row[10]
        
        # Create an inner dictionary for the spec if it doesn't exist yet
        if spec_num not in cable:
            cable[spec_num] = {}
        
        # Store the full cable data under the spec + size combination
        cable[spec_num][conductor_size] = {
            'Stock Number': row[2],
            'Overall Diameter': row[49],
            'Diameter Over Conductor': row[40],
            'Conductor Size': conductor_size
        }

3. Fix #2: Rewrite the Lookup Logic to Match the New Structure

Your find_cable function's condition was checking the wrong place. Let's update it to correctly validate spec and size, plus fix the "no spec" flow (since diameter is a property value, not a dictionary key):

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 != "no":
        # Check if the spec exists AND the size exists under that spec
        if spec in cable and size in cable[spec]:
            print(cable[spec][size])
            find_equation()
        else:
            print(f"Unable to find cable with Spec Number '{spec}' and Size '{size}'.")
            find_cable()
    else:
        # For the "no spec" case, we need to search all cables for matching diameter + size
        diameter = raw_input("Type the Overall Diameter of the cable.\n>")
        found = False
        
        for spec_data in cable.values():
            for cable_details in spec_data.values():
                if cable_details['Overall Diameter'] == diameter and cable_details['Conductor Size'] == size:
                    print(cable_details)
                    find_equation()
                    found = True
                    break
            if found:
                break
        
        if not found:
            print("No cable found matching that diameter and size.")
            find_cable()

Quick Notes to Avoid Headaches

  • If your CSV has a header row, don't forget to add next(reader) right after initializing the reader object—otherwise the header will be treated as a cable entry and break lookups.
  • raw_input is Python 2 syntax. If you're using Python 3, replace all instances with input().
  • Double-check your CSV column indices (like row[1], row[10]) to make sure they actually correspond to the spec number and conductor size columns—off-by-one errors are super common here!

内容的提问来源于stack exchange,提问作者JordanDotPy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:29:08