-1

I have this output

[u'JACK', u'', u'ROSE', u'', u'JANE']

I want to remove the unicode from each element of a list and join them into a single string like that:

Output:

['JACK ROSE JANE']
SiHa
  • 7,830
  • 13
  • 34
  • 43
kyle1009
  • 97
  • 1
  • 1
  • 5
  • 2
    Why do you want to get rid of the u? What have you tried so far? – Moberg Nov 09 '16 at 08:12
  • Possible duplicate of [Convert a Unicode string to a string in Python (containing extra symbols)](http://stackoverflow.com/questions/1207457/convert-a-unicode-string-to-a-string-in-python-containing-extra-symbols) – Moberg Nov 09 '16 at 08:13
  • 1
    Note that the 'u' is not a character, it just describes the ' spark. So, instead of seeing `'stuff'`, you see `u'stuff'`, because the string is a unicode string, but its content is still `stuff` – Right leg Nov 09 '16 at 08:14

6 Answers6

1

Previous answers are perfect. Just to show you another way:

result = [str(' '.join(filter(None, [u'JACK', u'', u'ROSE', u'', u'JANE'])))]

That's how to do it in functional paradigm :) And it looks nice, huh?

Actually, you don't need to worry about 'u' prefix. It simply let's you(as a developer) know that string is represented as unicode. I've added "str" to hide it(convert to ascii string) but it doesn't really needed. Please check this answer: What does the 'u' symbol mean in front of string values?

Community
  • 1
  • 1
Andrii Rusanov
  • 4,405
  • 2
  • 34
  • 54
1

There are many ways to program it. I prefer this:

output = str(" ".join(filter(None,[u'JACK', u'', u'ROSE', u'', u'JANE'])))

If you need list:

output = [str(" ".join(filter(None,[u'JACK', u'', u'ROSE', u'', u'JANE'])))]
Nom
  • 21
  • 2
-1

In Python 2, use the str function to convert a unicode string to a normal string:

>>> unicodeString = u'stuff'
>>> normalString = str(unicodeString)
>>> print(unicodeString)
u'stuff'
>>> print(normalString)
'stuff'
Right leg
  • 16,080
  • 7
  • 48
  • 81
-1
>>> in_list = list(filter(lambda x: len(x)>0, [u'JACK', u'', u'ROSE', u'', u'JANE']))
>>> output = " ".join(in_list)                                                                   
>>> output
'JACK ROSE JANE'
mandrewcito
  • 170
  • 8
-1

just for the sake of simplicity:

output = [' '.join([str(a) for a in [u'JACK', u'JANE', u'ROSE']])]
['JACK JANE ROSE']

tested in python2.7 and python3.5

-1

By using None in the filter() call, it removes all falsy elements. already by @Andrey

a = [u'JACK', u'', u'ROSE', u'', u'JANE']

c = [str(' '.join(filter(None, a)))]
print (c)

or iterate and get only True items, the contra argument here is the possibility of the string operators, o.lower,o.upper.....

b = [' '.join([o for o in a if o])]
print (b)
Ari Gold
  • 1,528
  • 11
  • 18