字符串格式-法国口音

M. *_*pée 0 python web-crawler scrapy

我在从Wikipedia抓取一些字符串数据时遇到问题。这是我的代码:

import scrapy
import json

class communes_spider(scrapy.Spider):
    name = "city"
    start_urls = ['https://fr.wikipedia.org/wiki/Liste_des_communes_de_Belgique_par_population']

    def parse(self, response):
        for city in response.css('table.wikitable td a::text').getall():
            if city == '2':
                pass
            elif city == '3':
                pass
            else:        
                yield {
                    'cities': city + ', BE'
                }
Run Code Online (Sandbox Code Playgroud)

问题在于字符串是法语,有些城市包含“è”或“é”。当我将它们导出到json文件时,像“ Li \ u00e8ge”这样的单词被导出为“Liège”。如何将那些弦变成法语字母?

Oka*_*kay 5

您无需将它们转换为法语。他们是一样的。您可以按照以下方式在ipython中检查它们

In [1]: l2 = 'Liège'

In [2]: l2
Out[2]: 'Li\xc3\xa8ge'

In [3]: print(l2)
Liège
Run Code Online (Sandbox Code Playgroud)