我得到一個錯誤的格式輸出,當我有像「ä」,「ü」「ö」等字符。 我從excel表欄讀取名稱,有時會有Unicode字符串,我編碼爲UTF-8。我的簡化代碼:格式:UTF-8編碼時錯誤的字符串寬度
import xlrd
name1 = (xl_sheet.cell_value(row,5)).encode('utf8') # use this because this cell can have strings with chars like "ö"
name2 = (xl_sheet.cell_value(row,7)).encode('utf8')
print('{:<15} {:<15}'.format(name1,name2)),
當我不使用.encode,我得到這個錯誤:
'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)
我發現了一個類似的帖子:Python String format width wrong when characters like é or ö in the string,但我不知道如何實現在我的情況下!?
我的產出表是這樣的:
oabcd oabcd
öabcd oabcd
oabcd oabcd
當F.E. char'ö'在變量中,則輸出不正確。
該Excel文件具有CP-1252「Windows Unicode」編碼。
xlrd.open_workbook(filename).encoding的輸出是:utf_16_le。
這只是一個簡單的例子。我在程序的早些時候有一個變量。當我嘗試你的建議時,我得到了錯誤:UnicodeDecodeError:'ascii'編解碼器無法解碼位置1中的字節0xc3:序號不在範圍(128)中。因此我使用了.encode()。 – user3265764
那麼你以後爲用戶保存變量......實質上,你鏈接的帖子建議在unicode中做所有事情,並且不要打擾ascii和編碼的東西。這應該解決你在'print'語句中遇到的空間問題。你認爲你可以用違規代碼更新你的問題嗎? – Zizouz212