相关疑难解决方法(0)

ValueError: 需要 1 个以上的值才能解包,拆分一行

我在同一行有一个包含问题和答案的文件,我想将它们分开并将它们附加到自己的空列表中,但不断收到此错误:
builtins.ValueError: need more than 1 value to unpack

questions_list = []
answers_list = []

questions_file=open('qanda.txt','r')


for line in questions_file:
    line=line.strip()

    questions,answers =line.split(':')

    questions_list.append(questions)
    answers_list.append(answers)
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
2
解决办法
2万
查看次数

Python 3.6 Split() 没有足够的值来解压解决方法?

当我尝试拆分单个单词时,我一直在 Python 中遇到错误。从我读到的,这是因为默认的 split() 命令查找空格。问题是,我希望第二个分配的变量(在这种情况下为资产)不返回任何内容或返回空值。这就是我正在使用的:

slack_text.startswith("!help"):
command, asset = slack_text.split() 
    if asset != "":
        if asset == "commandlist":
            slack_reply = "Available Commands: !addme, !getBalance, !buy <asset> <quantity>"
        elif asset == "ships":
            slack_reply = getAllShips()
        elif asset == "buildings":
            slack_reply = getAllBuildings()
        elif shipExists(asset):
                        slack_reply = getShip(asset)
        elif buildingExists(asset):
             slack_reply = getBuilding(asset)
        else:
             slack_reply = "Not a valid asset."
    else:
        slack_reply = "Available help modifiers are: commandlist, <ship_name>, <building_name>. (!help <modifier>)"
Run Code Online (Sandbox Code Playgroud)

因此,使用此代码,我可以在 Slack 中键入 '!help ship' 并且没有错误并返回 getAllShips() 函数。但是如果我简单地输入 '!help',Python 就会抛出一个错误。 …

python python-3.x

1
推荐指数
2
解决办法
2253
查看次数

标签 统计

python ×2

python-3.x ×1