Reputation: 35
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.
Upvotes: 1
Views: 7271
Reputation: 213223
You need to split the string on ,
:
number, type, identifier, height, width = mystring.split(',')
Upvotes: 0
Reputation: 99620
You need to split the string.
number, type, identifier, height, width = mystring.split(',')
Read more here
Here, split
would return a list
of the individual elements from the string split on ,
Demo:
>>> x = "9532167,box,C,5,20"
>>> x.split(',')
['9532167', 'box', 'C', '5', '20']
>>> number, type, identifier, height, width = x.split(',')
>>>
Upvotes: 6