【发布时间】:2021-08-05 17:55:15
【问题描述】:
我有一个清单,
list1 = [<td>-267</td>, <td>1,420</td>, <td>1,997</td>, <td>1,241</td>]
我想从里面的项目中删除 <td> 和 </td> ,所以我这样做了:
# Created empty list:
final = []
# then,
for i in list1:
i.replace('<td>', '').replace('</td>', '')
final.append(i)
给出错误 TypeError: 'NoneType' object is not callable while appending to empty list。
这里很好用:
a = '<td>-267</td>'
a.replace('<td>', '').replace('</td>', '')
输出 = '-267'
为什么它不适用于 append 方法?
【问题讨论】:
-
您缺少赋值语句,
i = i.replace('<td>', '').replace('</td>', '')然后追加或只是final.append(i.replace('<td>', '').replace('</td>', '')) -
这看起来像 XY 问题。您是如何首先获得列表的?看起来您尝试解析 html 源代码,所以请使用 BeautifulSoup。
标签: python string list for-loop append