混合整数和 unicode 时出现另一个 UnicodeEncodeError
Another UnicodeEncodeError when mixing integers and unicode
我的代码:
import openpyxl
file_location = "test.xlsx"
wb = openpyxl.load_workbook(file_location, use_iterators=True)
sheet = wb.get_sheet_by_name("Blad1")
count = 1
rows_count = sheet.max_row
while count < rows_count:
print sheet["C" + str(count)].value + " " + str(sheet["D" + str(count)].value) + " " + str(sheet["E" + str(count)].value) + " " + sheet["F" + str(count)].value
count = count + 1
我正在尝试读取 .xlsx 文件。一切都打印得很好,直到我到达一个有字符“Ä”和数字的单元格,我得到这个错误。
print sheet["C" + str(count)].value + " " + str(sheet["D" + str(count)].value) + " " + str(sheet["E" + str(count)].value) + " " + sheet["F" + str(count)].value
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 11: ordinal not in range(128)
我想我知道问题出在哪里,但我不知道如何解决。我正在使用 Python 2.7
尝试使用 str.encode() 而不是 str()。
myString.encode(encoding='UTF-8',errors='strict')
此 ID 是因为 ASCII
字符集
不处理“Ä”
str()
尝试将其转换为 ASCII
字符,因此抛出错误而不是尝试 encode()
并且“Ä”由 utf-8
处理,因此使用该方法
a=u"Ä"
a
u'\xc4'
str(a)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-368-202e444820fd> in <module>()
----> 1 str(a)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 0: ordinal not in range(128)
a.encode("utf-8")
'\xc3\x84'
print a.encode("utf-8")
问题在于您将 unicode 字符串传递给 print 语句。
尝试这样的事情:
for row in ws.iter_rows(): # let openpyxl worry about rows
print " ".join(cell.value.encode("utf-8") for cell in row[3:7])
我的错。我刚刚删除了 str() 并且一切正常!感谢所有试图提供帮助的人!
我的代码:
import openpyxl
file_location = "test.xlsx"
wb = openpyxl.load_workbook(file_location, use_iterators=True)
sheet = wb.get_sheet_by_name("Blad1")
count = 1
rows_count = sheet.max_row
while count < rows_count:
print sheet["C" + str(count)].value + " " + str(sheet["D" + str(count)].value) + " " + str(sheet["E" + str(count)].value) + " " + sheet["F" + str(count)].value
count = count + 1
我正在尝试读取 .xlsx 文件。一切都打印得很好,直到我到达一个有字符“Ä”和数字的单元格,我得到这个错误。
print sheet["C" + str(count)].value + " " + str(sheet["D" + str(count)].value) + " " + str(sheet["E" + str(count)].value) + " " + sheet["F" + str(count)].value
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 11: ordinal not in range(128)
我想我知道问题出在哪里,但我不知道如何解决。我正在使用 Python 2.7
尝试使用 str.encode() 而不是 str()。
myString.encode(encoding='UTF-8',errors='strict')
此 ID 是因为 ASCII
字符集
str()
尝试将其转换为 ASCII
字符,因此抛出错误而不是尝试 encode()
并且“Ä”由 utf-8
处理,因此使用该方法
a=u"Ä"
a
u'\xc4'
str(a)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-368-202e444820fd> in <module>()
----> 1 str(a)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 0: ordinal not in range(128)
a.encode("utf-8")
'\xc3\x84'
print a.encode("utf-8")
问题在于您将 unicode 字符串传递给 print 语句。
尝试这样的事情:
for row in ws.iter_rows(): # let openpyxl worry about rows
print " ".join(cell.value.encode("utf-8") for cell in row[3:7])
我的错。我刚刚删除了 str() 并且一切正常!感谢所有试图提供帮助的人!