UnicodeDecodeError:'ascii'编解码器无法解码位置8的字节0xd0:序数不在范围内(128)

时间:2013-06-04 09:02:42

标签: unicode python-2.7 python-unicode

我一直在尝试用Python编写一个程序,从Excel文件中读取单元格值,并将单元格的内容从爱沙尼亚语翻译成英语或俄语,并将它们组合成一个单独的字符串。结果将打印到文本文件中。爱沙尼亚语 - >英语似乎工作正常,但俄语错误开始出现:

Traceback (most recent call last):   
     File "erid.py", line 140, in <module>
      f.write(aNimed(row_index, 1, 'ru')+ '\n')
      File "erid.py", line 120, in aNimed
    nimi += komponendid[i].strip() 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 8: ordinal not in range(128)

 Traceback (most recent call last):
 File "erid.py", line 140, in <module>
     f.write(aNimed(row_index, 1, 'ru')+ '\n')   File "erid.py", line 120, in aNimed
     nimi = nimi + komponendid[i][1:].strip()
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 9: ordinal not in
 range(128)

第一个由“antiakteriaalne”一词触发,第二个由“+hoobkäepide”触发。我怀疑“+”符号是导致第二种情况出现问题的原因,而不是“ä”。一些俄罗斯人物似乎是一个问题,而有些则没有。我有点想法。

Python代码:

# -*- coding: utf-8 -*-
from xlrd import open_workbook, cellname, XL_CELL_TEXT 
from xlwt import Workbook
from xlutils.copy import copy
import sonaraamatud #dictionary

# open file with data
book = open_workbook('Datafile.xls')
# Safe write unicode to ignore unicode errors
# http://developer.plone.org/troubleshooting/unicode.html
def safe_write(failName, word):
    if type(word) == str:
        failName.write(word + '\n')
    else:
        failName.write(word.decode("utf-8") + '\n')

def safeDecode(word):
    if type(word) == str:
        word = unicode(word, 'utf-8', errors='ignore')
        return word
    else:
        word = unicode(word)
        return word

# Translate surface coating name
def translatePind(langa, langb, word):
        answ = ""
        if (sonaraamatud.kasOlemas3(langa, sonaraamatud.pinnaKatted) == True):
                answ = langa
                return answ
        #if langa is Estonian
        if (langa == 'et'):
                # if langb is english
                if (langb == 'en'):
                        try:
                                answ = sonaraamatud.pinnakattedEstEng[word]
                        except KeyError:
                                answ = word
                # If lang b is russian
                elif (langb == "ru"):
                        try:
                                answ = sonaraamatud.pinnakattedEngRus[sonaraamatud.pinnakattedEstEng[word]]
                        except KeyError:
                                answ = word

        # if langa is english
        elif (langa == "en"):
                # if langb is Estonian
                if (langb == "et"):
                        try:
                                answ = sonaraamatud.pinnakattedEngEst[word]
                        except KeyError:
                                answ = word
                # if langb is Russian
                elif (langb == "ru"):
                        try:
                                answ = sonaraamatud.pinnakattedEngRus[word]
                        except KeyError:
                                answ = "KeyError"
        return answ

def aNimed(row, sheetNr, lang):
        #  Function combines name
        #  name: aNimed
        #  @param: rida, lehe number
        #  @return: Product name
        #vali leht (worksheet)
        sheet = book.sheet_by_index(sheetNr) #sheetNr
        komponendid = []
        nimi = ""
        if (lang == 'et'):
        komponendid.append(str(sheet.cell(row, 5).value)) # Model
                komponendid.append('(' + sheet.cell(row, 6).value + ')')#surface
                komponendid.append(sheet.cell(row, 7).value) #extras
        elif (lang == 'en'):
                komponendid.append(str(sheet.cell(row, 5).value)) # Mudel
                komponendid.append('(' + translatePind('et', 'en', sheet.cell(row, 6).value) + ')')
                komponendid.append(sheet.cell(row, 7).value) #lisad
        elif (lang == 'ru'):
                """
                Alternativ method trying to use safeDecode, NOT working! 
                komponendid.append(str(safeDecode(sheet.cell(row, 5).value))) # Mudel
                surface= safeDecode(sheet.cell(row, 6).value)
                komponendid.append('(' + translatePind('et', 'ru', str(surface)) + ')')
                komponendid.append(safeDecode(sheet.cell(row, 7).value)) #lisad
                """
                komponendid.append(str(sheet.cell(row, 5).value)) # Mudel
                komponendid.append('(' + translatePind('et', 'ru',sheet.cell(row, 6).value) + ')')
                komponendid.append(sheet.cell(row, 7).value) #lisad
        pikkus = len(komponendid)

        print(komponendid)
        for i in range(0, pikkus):
                if (komponendid[i] == "" or komponendid[i] == "()" or komponendid[i] == " "):
                        i+=1
                        continue
                elif (i == pikkus-1 and komponendid[i][0] != " "):
                        print("1"+ komponendid[i])
                        nimi += komponendid[i].strip()
                        i+=1
                elif (komponendid[i][0] == " " and komponendid[i][1]== "+"):
                        #print("2"+ komponendid[i])
                        nimi = nimi + komponendid[i][1:].strip()
                        i+=1
                else :
                        #print("4"+ komponendid[i])
                        nimi = nimi + komponendid[i].strip() + " "
                        i+=1
        return nimi

# Use: aNimed(row, sheetNr, lang)
sheet = book.sheet_by_index(7)
f= open('data.txt', 'w')
for row_index in range (1, sheet.nrows):
    #print(aNimed(row_index, 5, 'en'))
    f.write(aNimed(row_index, 1, 'ru')+ '\n')
    #safe_write(f, aNimed(row_index, 1, 'ru'))
f.close()

1 个答案:

答案 0 :(得分:0)

它不是特别优雅,但我想我有一个解决方法。而不是从excel文件中读取,而是从csv文件中读取。例如,

`import csv
data = []
opened_file = open(csv_filename, 'rb')
reader = csv.reader(opened_file)
for row in reader:
    data.append(row)
opened_file.close()`

现在您将数据保存为列表。你翻译并将其保存为不同的列表,比如translated_data。现在,这是关键,你可以用

打开一个新的工作簿
`from xlwt import Workbook
book = Workbook(encoding="utf8")
foo = book.add_sheet("foo")
for row_num in range(len(translated_data)):
    for col_num in range(len(translated_data[row_num]):
        foo.write(row_num, col_num, translated_data[row_num][col_num]
book.save("filename.xls")`  

关键是如果你使用Workbook()就可以指定编码,但是如果使用open_workbook(),你似乎仍然坚持使用ascii。