1

I understand there are many non ascii characters questions on stackoverflow but since I'm a total newb I've had no luck in successfully implementing them, plus I find the whole 'unicode' concept difficult to understand.

So I have a list -

mylist = ["apple", "samsung", "toshiba", "Don’t know", "Can’t recall"] 

I would like to access the single quote marks at index 3 and 4 and replace them with an apostrophe.

I tried this:

# -*- coding: utf-8 -*-
mylist = ["hello", "don't know", "Don’t know", "Can't recall"]
for word in mylist:
    word.replace(u"’", "'")
print mylist

I get the following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 3: ordinal not in range(128)

Not sure if this is useful but I am using python version 2.x and I know that this problem may not occur if I was using version 3.

Thanks!

1 Answer 1

1
>>> mylist = ["apple", "samsung", "toshiba", "Don’t know", "Can’t recall"]
>>> [item.replace('\xe2\x80\x99',"'") for item in mylist]
['apple', 'samsung', 'toshiba', "Don't know", "Can't recall"]

If all the items are already unicode:

>>> mylist = [u"apple", u"samsung", u"toshiba", u"Don’t know", u"Can’t recall"]
>>> [item.replace(u'’',u"'") for item in mylist]
[u'apple', u'samsung', u'toshiba', u"Don't know", u"Can't recall"]
Sign up to request clarification or add additional context in comments.

3 Comments

Tried both and I either get an error or this: ['hello', "don't know", 'Don\xe2\x80\x99t know', "Can't recall"] strange.
@Boosted_d16 works fine for me, don't forget to use this line : # -*- coding: utf-8 -*- in your source code. ideone.com/HSlDx5
Had no idea why it wasn't working before but its working fine now. Cheers Ashwini.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.