python - Split line into category and text -


i have list looks this:

foo = ["neg * , sentence","pos * , sentence"] 

i need split sentences in such way 1 value become category, neg or pos, , 1 sentence. tried:

for text in foo:     text = text.split("*")     a,b in text:         cat=a         text=b 

however "too many values unpack", have idea?

your problem loop horribly constructed (which excusable, since new whole thing)

try safer method (a list-comprehension):

>>> foo = ["neg * , sentence","pos * , sentence"] >>> [p.split('*', 1) p in foo] [['neg ', ' , sentence'], ['pos ', ' , sentence']] 

now have list of [cat, text] items.

>>> l = [p.split('*', 1) p in foo] >>> cat, text in l:     print 'cat: %s, text: %s' % (cat, text)  cat: neg , text:  , sentence cat: pos , text:  , sentence 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -