Python3 TypeError:只能将列表(不是“str”)连接到列表

问题描述 投票:-1回答:2

我正在将odoo 11,python 2.7移植到python 3.我已经编辑了一个属于odoo,python代码的插件。

代码是:

vat = invoice.partner_id.vat or ''
vat = list(filter(lambda x: x.isnumeric(), vat[:2])) + vat[2:]

错误是:

TypeError: can only concatenate list (not "str") to list

我该如何解决这个问题,这个代码有什么问题?请帮我。

python python-3.x python-2.7 odoo odoo-11
2个回答
1
投票
list(filter(lambda x: x.isnumeric(), vat[:2]))

上面的操作总是返回列表。

vat = invoice.partner_id.vat or '' 
  • 看来,这个操作返回str(因为or '')。

如果你期望你的type(vat)==list,你应该使用

vat = invoice.partner_id.vat or []

如果您期望type(vat)==str,您应该将过滤后的列表转换为str

"".join(list(filter(lambda x: x.isnumeric(), vat[:2]))) + vat[2:]

-1
投票

使用线

vat = invoice.partner_id.vat or ''
#convert string to list
vat = [x for x in val]
vat = list(filter(lambda x: x.isnumeric(), vat[:2])) + vat[2:]

首先将字符串转换为列表。

© www.soinside.com 2019 - 2024. All rights reserved.