簡體   English   中英

如何在python中編碼字符'\\ u0107'

[英]How to encode character '\u0107' in python

我正在嘗試從Wikipedia頁面(這是某些年份的前100首單曲)中抓取數據,同時將輸出保存到1951-1959年得到的csv中,然后給出了一個錯誤:

在writer.writerow(songs)文件“ C:\\ Python36_64 \\ lib \\ encodings \\ cp1252.py”中的第43行,

第19行,以編碼返回codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError:'charmap'編解碼器無法在位置29編碼字符'\\ u0107':字符映射到<undefined>

碼:


from bs4 import BeautifulSoup
import requests
import csv

data = []


def scrape_data(search_year):
    year_data = []
    url = f'https://en.wikipedia.org/wiki/Billboard_Year-End_Hot_100_singles_of_{str(search_year)}'
    # Get a source code from url
    r = requests.get(url).text
    soup = BeautifulSoup(r, 'html.parser')
    # Isolate the table part from the source code
    table = soup.find('table', attrs={'class': 'wikitable'})
    # Extract every row of the table
    rows = table.find_all('tr')

    # Iterate through every row
    for row in rows[1:]:
        # Extract cols (with tags td and th)
        cols = row.find_all(['td', 'th'])
        # List comprehension (create a list of lists, list of rows, in which every row is a list of table text)
        year_data.append([col.text.replace('\n', '') for col in cols])

    # Add the year, this data is from to the beginning of the list
    for n in year_data:
        n.insert(0, search_year)
    return year_data


for year in range(1951, 2019):
    try:
        data.append(scrape_data(year))
        print(f'Year {str(year)} Scrapped')
    except AttributeError as e:
        print(f'Year {str(year)} is not aviable')

writer = csv.writer(open('songs.csv', 'w'), delimiter=',', lineterminator='\n', quotechar='"')
for year_data in data:
    for songs in year_data:
        writer.writerow(songs)
        print(songs)

我認為您可以在編寫輸出時通過使用正確的unicode編碼來糾正此問題:

writer = csv.writer(open('songs.csv', 'w', encoding='utf-8'),
                    delimiter=',', lineterminator='\n', quotechar='"')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM