Python-split()命令可以被索引吗?

Python-split()命令可以被索引吗?,python,csv,split,Python,Csv,Split,我正在尝试分析和拆分该行: redfish - 12,000 lbs - trade for SNE stocks 我正在尝试的代码是: elif ('-' in line) and ('lbs' in line): fish, remainder = line.split('-') #splits line into two halves at the - (fish to one side) #print("line.split is:", li

我正在尝试分析和拆分该行:

redfish - 12,000 lbs - trade for SNE stocks
我正在尝试的代码是:

elif ('-' in line) and ('lbs' in line):
    fish, remainder = line.split('-')               #splits line into two halves at the - (fish to one side)
    #print("line.split is:", line.split(':'))
    if '@' in remainder:
        weight, price = remainder.split('@')        #splits already split piece (remainder) into two halves at @
        if '-->' in price:
            price, junk = price.split('-->')
    if 'trade' in remainder:
        if 'to ' in remainder:
            weight, price = remainder.split('to ')
        elif ' or ' in remainder:
            weight, price = remainder.split(' or ') #add spaces around ' or ' so we don't match 'for'
    if 'swap' in remainder:
        weight, price = remainder.split('to ')
它在线路上出现故障:

redfish - 12,000 lbs - trade for SNE stocks
fish,余数=line.split('-')

错误如下:

ValueError:要解压缩的值太多(预期为2个)

现在我知道这是由于该行中有2个
'-'
,Python不知道在哪一个上进行拆分,所以我尝试告诉它在第一个
'-'
上进行拆分,方法是:
fish,resident=line.split('-'[0])
,但失败了

所以我的问题是:有办法解决这个问题吗?我是否可以用另一种方法索引
split()
命令,以便按我的意愿成功拆分该行


感谢您提供的帮助或提示。

您很接近,请尝试使用:

line.split('-',1)

它告诉它只在遇到第一个“-”时拆分字符串

但是,如果只想在第二个命令上进行拆分,我不知道是否可以直接“索引”您的拆分命令


在这种情况下,我建议将整个字符串拆分,然后加入所需的部分。

哦,好了。我知道这将是一个简单的答案。是的,我想我以后会在剩下的部分中分到第二个。不过谢谢你的回答:)我会在几分钟内点击复选标记接受它,如果它允许的话