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

Python代码报错‘list’ object has no attribute ‘split’的解决咨询

Fixing the AttributeError: 'list' object has no attribute 'split'

Hey there! I see you're just starting out with Python and databases—let's break down what's going wrong and fix this together.

The Root of the Problem

When you run c.fetchall(), it returns a list of tuples (even if your query only finds one fund). So your variable s looks something like this under the hood:

[['Global Growth Fund Cap (EUR)']]

When you call s[0], you're grabbing the inner list ['Global Growth Fund Cap (EUR)']—not a string. That's why .split() throws an error: lists don't have that method!

How to Fix It

We just need to extract the actual fund name string from that nested structure first. Here are two straightforward ways to do this:

Option 1: Use fetchone() (Best for Single Results)

Since you're querying by ISIN (which should return exactly one fund), fetchone() is more efficient—it returns a single tuple with your result instead of a list of tuples. Here's the updated code:

# Don't forget to import and connect to your database first!
import sqlite3
conn = sqlite3.connect('your_funds_db.db')
c = conn.cursor()

isin = raw_input("isin of the fund? ")
# Fetch the single result tuple directly
result_tuple = c.execute("select name from funds where isin like ?", ('%'+isin+'%',)).fetchone()

if result_tuple:  # Make sure we found a fund to avoid errors
    # Extract the string from the tuple
    fund_name = result_tuple[0]
    
    # Process stop words as you intended
    stop_words = ['Cap','Ptf', '(EUR)', 'EUR', 'USD', '(D)', 'A', 'B', 'C', 'D', 'I', 'E' ]
    final_list = []
    for word in fund_name.split():
        if word not in stop_words:
            final_list.append(word)
    print(" ".join(final_list))
else:
    print("No fund found with that ISIN.")

# Clean up the database connection
conn.close()

Option 2: Stick with fetchall() (For Multiple Results)

If you ever expect your query to return multiple funds, adjust your code to loop through each result and extract the name string:

isin = raw_input("isin of the fund? ")
c.execute("select name from funds where isin like ?", ('%'+isin+'%',))
results = c.fetchall()

stop_words = ['Cap','Ptf', '(EUR)', 'EUR', 'USD', '(D)', 'A', 'B', 'C', 'D', 'I', 'E' ]

# Loop through each result tuple in the list
for result in results:
    fund_name = result[0]  # Pull the string from the tuple
    final_list = []
    for word in fund_name.split():
        if word not in stop_words:
            final_list.append(word)
    print(" ".join(final_list))

Quick Recap

  • fetchall() returns a list of tuples: [(row1_col1, row1_col2), (row2_col1, row2_col2), ...]
  • fetchone() returns a single tuple for the first result (or None if no matches)
  • Always confirm you're working with a string before using string methods like .split()

内容的提问来源于stack exchange,提问作者J. Malik

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:53:40