in Education by
Currently I am working on this code: customer_telno = customer.find('div', 'customer_phone_number') customer_telno = '' if customer_telno is None else customer_telno.contents[0] p.customer_info = str(customer_phone + ' ' + customer_telno).strip() When the above snippet is run I get UnicodeEncodeError : Traceback (most recent call last): File "foobar.py", line 792, in p.customer_info = str(customer_phone + ' ' + customer_telno).strip() UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128) I have tried using everything possible but couldn’t find anything which works consistently. Can anyone solve this problem? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
@Ankita ,This error is occurring mainly because you are converting unicode to encoded bytes using str, so to solve the problem you need to stop str and instead use .encode() to properly encode the strings. Syntax- str.encode(encoding="utf-8",errors="strict") It returns a encoded version of a string as a bytes object. Here, an error is set to specify different error handling scheme and by default, the error is set to “strict” which means that the encoding error raises a UnicodeError, the default encoding is set to “utf-8”. Using .encode() in your code: p.customer_info = u' '.join((customer_phone, customer_telno)).encode('utf-8').strip() if you wish to know what is python visit this Python tutorial and Python Interview questions.

Related questions

0 votes
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128) ... anything which works consistently. Can anyone solve this problem?...
asked Nov 20, 2020 in Education by Editorial Staff
0 votes
    What makes using range() to make the process of generating a quadrillion values in a instant of time, I am amazed by ... o += 1 return Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a Selenium Python automated regression test script running on our 64bit Server on IE11, Windows ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I want to get the search result using scrapy post request after giving the input to CP Number as 16308 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    Here is a pure Python-specific design question: class MyClass(object): ... def get_my_attr(self): ... def ... you use and why? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Please tell me what random.seed() does in Python. For example, why do the below trials do what they do ( ... randint(1, 10) 7 Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    This my my JSON: { "maps": [ { "id": "AT", "iscategorical": "0" }, { "id": "AT", "iscategorical": "0 ... the values, How can I do it? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Which of the following is not a valid encoding scheme for character?a. ASCII b. ISCII c. Unicode and. ASCII Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Is there a way to to disable intellisense in ax2012's code editor? sometimes when writing selecting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I am using the Google Sheets V4 Values collection and I am having trouble figuring out how to get each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I was developing one web application in cakephp 2.2.3. that application I was using CakeEmail. But Now ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
...