【问题标题】:trouble with appending unicode object to list将 unicode 对象附加到列表时遇到问题
【发布时间】:2014-01-25 19:40:10
【问题描述】:

我很想从这个 xml 提要导入 http://www.lnv.fr/xml/ajaccio/calendrier.xml 我遇到了一些麻烦,因为我要提取的一些数据有法语重​​音符号。

url = 'http://www.lnv.fr/xml/ajaccio/calendrier.xml'
r = requests.get(url)
soup = BeautifulSoup(r.content)
matches = soup.findAll('match')

当我这样做时

for match in matches:
    print match.equipedomicile.string

它将它们打印出来,因为对于带有重音符号(例如 Sète)的团队来说没有问题。

但是当我这样做时

def GetGames():
homeTeamList = []    
for match in matches:
    homeTeam = unicode(match.equipedomicile.text)        
    homeTeamList.append(homeTeam)
return homeTeamList

并调用带有重音符号的列表团队的功能不正确。即赛特 现在变成 u'S\xe8te'

【问题讨论】:

    标签: python xml python-2.7 unicode python-2.x


    【解决方案1】:

    您得到的是 unicode 字符串的 repr 版本,在列表的各个元素上使用 print,您将获得正确的输出。

    >>> a = [u'S\xe8te']
    >>> a
    [u'S\xe8te']
    >>> print a[0]
    Sète
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2020-01-15
      • 2011-06-10
      • 2014-03-23
      相关资源
      最近更新 更多