我在将字符串格式化为utf-8时遇到了麻烦 在这个脚本中我从excel文件中获取数据 然后在循环中打印出来,问题就在于此 带有特殊字符的字符串显示错误。
结果我继续得到'PatrÄ«cija'而不是'Patrīcija' 似乎无法找到解决此问题的方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import xlrd
import datetime
def todaysnames():
todaysdate = datetime.datetime.strftime(datetime.date.today(), "%d.%m")
book = xlrd.open_workbook("vardadienas.xls")
sheet = book.sheet_by_name('Calendar')
for rownr in range(sheet.nrows):
if sheet.cell(rownr, 0).value == todaysdate:
string = (sheet.cell(rownr, 1).value)
string = string.encode(encoding="UTF-8",errors="strict")
names = string.split(', ')
return names
names = todaysnames()
for name in names:
print name
答案 0 :(得分:1)
将编码更改为iso8859_13(Baltic languages)
并修复了它。
答案 1 :(得分:-1)
我认为您的问题可能是由print
造成的。 xlrd返回utf8。根据控制台的编码,print
可能无法正确打印。我有时会在法语Windows(编码为cp1252)上注意到这一点
以下问题:Python, Unicode, and the Windows console解释了如何在Windows上的控制台上打印unicode char。我没试过自己,但看起来不错。
我希望它有所帮助