UnicodeEncodeError: ascii codec cannot encode character u '\ u2019' at position 6: serial number not in range (128)

I am trying to get a list of 500 restaurants in Amsterdam from TripAdvisor; however after restaurant 308 i get the following error:

Traceback (most recent call last):
  File "C:/Users/dtrinh/PycharmProjects/TripAdvisorData/LinkPull-HK.py", line 43, in <module>
    writer.writerow(rest_array)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 6: ordinal not in range(128)

I tried a few things that I found in StackOverflow, but now nothing works. I was wondering if anyone could take a look at my code and see some potential solutions that would be great.

        for item in soup2.findAll('div', attrs={'class', 'title'}):
            if 'Cuisine' in item.text:
                item.text.strip()
                content = item.findNext('div', attrs=('class', 'content'))
                cuisine_type = content.text.encode('utf8', 'ignore').strip().split(r'\xa0')
        rest_array = [account_name, rest_address, postcode, phonenumber, cuisine_type]
        #print rest_array
        with open('ListingsPull-Amsterdam.csv', 'a') as file:
                writer = csv.writer(file)
                writer.writerow(rest_array)
    break
+4
source share
2 answers

rest_array Unicode. csv.writer , ( Python 2.7).

"utf8":

with open('ListingsPull-Amsterdam.csv', mode='a') as fd:
    writer = csv.writer(fd)
    rest_array = [text.encode("utf8") for text in rest_array]
    writer.writerow(rest_array)

: file , file() ( open()).

CSV Microsoft Excel, , , "cp1252" ( u "\ u2019" ).

+7

non-ascii csv. , , (). UTF-8. :

with open('ListingsPull-Amsterdam.csv', 'a', encoding='utf-8') as file:
    writer = csv.writer(file)
    writer.writerow(rest_array)

Python 3.x, .

+2

Source: https://habr.com/ru/post/1660855/


All Articles