1

I am looking to take a list or string that when printed looks like this:

9532167,box,C,5,20

and assign each comma delimited value to a set variable like so:

var1=9532167
var2=box
var3=C
var4=5
var5=20

(note-this isn't code, but how I want the pieces of string to be assigned to the variables)

Currently I have:

var1, var2, var3, var4, var5 = mystring

but get this error:

exceptions.ValueError: too many values to unpack

I realize it is a very simple question, but no amount of searching has turned up something I can understand. I'm a newb to Python.

Thank you for any help.

4

2 に答える 2

6

文字列を分割する必要があります。

number, type, identifier, height, width = mystring.split(',')

詳細はこちら

ここでは、分割された文字列から個々の要素のsplitを返しますlist,

デモ:

>>> x = "9532167,box,C,5,20"
>>> x.split(',')
['9532167', 'box', 'C', '5', '20']
>>> number, type, identifier, height, width = x.split(',')
>>>
于 2013-10-11T15:18:30.370 に答える
0

次の文字列を分割する必要があります,

number, type, identifier, height, width = mystring.split(',')
于 2013-10-11T15:18:35.183 に答える