0

I am working with a module called PyDictionary. Whenever it gets the synonym of a word, it creates a list of the synonyms, however when I try print the list, it has a 'u' before the synonym, example:

[u'welcome', u'howdy', u'hi', u'greetings', u'bonjour']

I've tried: synonym = re.sub('u','',synonym[0]), and this works, but only prints 'welcome', not the entire list.

Any help would be greatly appreciated. Thanks!

Mohd
  • 5,523
  • 7
  • 19
  • 30
  • 1
    The 'u' in front just means it is a unicode string. https://stackoverflow.com/questions/11279331/what-does-the-u-symbol-mean-in-front-of-string-values – idjaw May 30 '17 at 23:45
  • That's Python's way of letting you know it's a Unicode string. When you print the string however, the `u` will not be visible, so it's nothing to worry about. – Christian Dean May 30 '17 at 23:46
  • `u` prefix means that the string is unicode. If you print it using the `print()` function it will not show that decorator (e.g. `print(your_list[0])`) – zwer May 30 '17 at 23:46
  • I'm using PyTTSX to read the synonyms. Using print(synonym[0]) prints a single item from the list, the first item. print(synonym[0:len(synonym)]) prints all the items, but the u is there. Thanks for the help anyway. –  May 30 '17 at 23:50
  • 1
    @ElliotPoots - print it in a loop, e.g.: `for syn in synonym: print(syn)` or if you want single-line: `print(", ".join(synonym))` – zwer May 30 '17 at 23:54
  • @zwer That works. Thanks so much. Sorry, still learning the inns and outs of Python. Again, appreciated! –  May 30 '17 at 23:56
  • @zwer I am now doing something similar for the definition. I was wondering how I'd do it, I've attempted the fix for the synonym here, but it does not work. `print("The definition for " + str(dWord) + " is: " + ", ".join(definition)) for defini in definition: s_engine.say("The definition for " + str(dWord) + " is " + str(definition)) s_engine.runAndWait()` However the output is either 'Verb' or 'Noun' or the dictionary with the u. –  May 31 '17 at 15:53

2 Answers2

0

If you want to convert the list to a list of strings you can use list comprehensions like:

result = [str(x) for x in synonym]
Mohd
  • 5,523
  • 7
  • 19
  • 30
0

The u you are seeing is not part of the string content, and therefore cannot be removed with regular expression substitution. Instead, it should be considered part of the opening quote. The syntax of the Python language itself allows for string literals to be defined with quotes that are inflected in this way. For example:

a = 'spam'
b = u'spam'   # in Python 2, a and b are different types

When you display the variable the way you would to a programmer, i.e. in such a way that the quote characters are visible (e.g. just typing synonym[0] at the prompt, or trying to print the whole list synonym, or otherwise invoking Python's repr() mechanism on the strings) then the u will be visible too. By contrast, when you display the variable as you would to a user (e.g. with print synonym[0], or by joining the list and then printing the resulting string) then, just as you would not expect to see the quote characters themselves, you will also not see the u.

jez
  • 14,867
  • 5
  • 37
  • 64