私が解析しようとして行を分割しています:Python - 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:", 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 ')
それはライン上で失敗します。
とfish, remainder = line.split('-')
エラー:
ValueError: too many values to unpack (expected 2)
。
今、私はこれがその行で2 '-'
があり、Pythonが上に分割するかを知らないので、私が最初に'-'
に分割するように指示しようとしたことに起因している知っている:fish, remainder = line.split('-'[0])
が、それが失敗しました。
私の質問です。これには方法がありますか? split()
コマンドに別の方法でインデックスを付けることができます。その結果、私は望みどおりにその行を正常に分割できますか?
ご協力いただきありがとうございます。あなたが接近していた
Ohh gotcha。私はそれが簡単な答えになることを知っていた。ええ、私は残りの部分に入ってから2番目のものに分割しなければならないと思っています。答えをありがとう:)私は数分でそれを受け入れるためにチェックマークをクリックします – theprowler