Python xlrd - Unicode错误

0 投票
1 回答
551 浏览
提问于 2025-04-17 23:26

我正在从Excel文件中读取数据,然后把这些数据写入Django的数据库。我使用的是Python的xlrd模块。

我遇到了以下错误:

'ascii' codec can't encode character u'\xc1' in position 6: ordinal not in range(128)

我尝试了很多解决办法,比如:

1) 我之前使用了str(variable),现在把它去掉了。现在直接把值存储到数据库里。

2) 尝试了wb = open_workbook('static/'+filename, encoding_override="utf_16_le")

3) .encode(error=replace)

但是都没有用。我该怎么解决这个错误呢?

这是我的代码

def __init__(self, arm_id, dsp_name, DSP, hubcode, Pincode, pptl,state):
        self.arm_id = arm_id
        self.dsp_name = dsp_name
        self.DSP = DSP.zfill(2)
        self.hubcode = hubcode
        self.Pincode = Pincode
        self.pptl = pptl
        self.state = state
wb = open_workbook('static/'+filename, encoding_override="utf_16_le")
for sheet in wb.sheets():
    number_of_rows = sheet.nrows
    number_of_columns = sheet.ncols

    items = []
    arm_list = []
    pptl_list = []
    pptlcode_list = []
    count = 1
    status = 0
    for row in range(1, number_of_rows):
        values = []
        for col in range(number_of_columns):
            value  = (sheet.cell(row,col).value)
            try: value = str(int(value))
            except ValueError: pass
            finally: values.append(value)

        item = Excel(*values)
        count +=1

        arm_id = item.arm_id
        if arm_id not in arm_list:
            description = 'Arm'+arm_id
            arm_obj = Arm(arm_id = arm_id, description = description)
            arm_obj.save()
            arm_list.append(arm_id)

        pptl_id = (item.pptl)
        if pptl_id not in pptl_list: 
            try : 
                pptl_obj = PPTLconfig.objects.get(pptl_id = pptl_id)
                pptl_obj.arm_id = arm_obj
                pptl_obj.hubcode = hubcode
            except : 
                description = 'PPTL'+pptl_id
                pptl_obj = PPTLconfig(pptl_id = pptl_id, description = description , arm_id = arm_obj, hubcode = (item.hubcode))
            finally : 
                pptl_obj.save()
                pptl_list.append(pptl_id)


        code = []

        for factors in SORTATION_FACTORS:
            if factors == 'DSP': code.append((item.DSP))
            elif factors == 'Pincode': code.append((item.Pincode))
            elif factors == 'DG': code.append((item.state).zfill(4))

        code = ','.join(code)

        if code not in pptlcode_list : 
            try : 
                code_obj = PPTLcode.objects.get(code = code)
                code_obj.pconf_id = pptl_obj
            except : code_obj = PPTLcode(code=code, pconf_id=pptl_obj)
            finally : 
                code_obj.save()
                pptlcode_list.append(code)

        else : 
            error = "Duplicate PPTLcode " + code + " at Row " + str(count)
            status = 1
            delete_data(1)
            return (status,error)

###############Add ArmPrinterMapping ######################


arm_obj_list = Arm.objects.all()

for arm_obj in arm_obj_list:
    printer_name = 'Arm'+str(arm_obj.arm_id)
    ap_mapping = ArmPrinterMapping(arm_id = arm_obj, printer_name = printer_name)
    ap_mapping.save()

return (0,0)

1 个回答

0

把默认编码设置为 utf8,这样应该就可以正常工作了。

reload(sys)
sys.setdefaultencoding('utf8')

撰写回答