UnicodeEncodeError: 'cp949' codec can't encode character '\u20a9' in position 90: illegal multibyte sequence

后端 未结 1 1701
礼貌的吻别
礼貌的吻别 2020-12-21 10:29

I\'m a python beginner.

I\'m trying to crawl google play store and export to csv file. But I got a error message.

UnicodeEncodeError: \'cp949\' codec         


        
相关标签:
1条回答
  • 2020-12-21 11:06

    Python 3 opens text files in the locale default encoding; if that encoding cannot handle the Unicode values you are trying to write to it, pick a different codec:

    with open('result.csv', 'w', encoding='UTF-8', newline='') as f:
    

    That'd encode any unicode strings to UTF-8 instead, an encoding which can handle all of the Unicode standard.

    Note that the csv module recommends you open files using newline='' to prevent newline translation.

    You also need to open the file just once, outside of the for loop:

    with open('result.csv', 'w') as f:  # Just use 'w' mode in 3.x
        fields = ('title', 'developer', 'developer_link', 'price', 'rating', 'reviewers',
                  'downloads', 'date_published', 'operating_system', 'content_rating',
                  'category')
        w = csv.DictWriter(f, )
        w.writeheader()
    
        for div in soup.findAll( 'div', {'class' : 'details'} ):
            #
            # build app_details
            #
    
            w.writerow(app_details)
    
    0 讨论(0)
提交回复
热议问题