数据库:TypeError:无法连接“str”和“list”对象

2024-10-01 00:32:48 发布

您现在位置:Python中文网/ 问答频道 /正文

`Traceback (most recent call last):
  File "fmcrawler_sql.py", line 317, in <module>
    crawl(initFighter=fighter,K=4)
  File "fmcrawler_sql.py", line 114, in crawl
    write_page_to_database(initFighterURL,cur)
  File "fmcrawler_sql.py", line 292, in write_page_to_database
    write_fights_to_database(fights,cur)
  File "fmcrawler_sql.py", line 211, in write_fights_to_database
    fightId = hash(bothFighters+fight['Event'])
  TypeError: cannot concatenate 'str' and 'list' objects
`

这些线路怎么了?你知道吗


Tags: toinpymostsqllinepagedatabase
2条回答

从你发布的内容看不出发生了什么,但是这个错误很常见。在实际代码的某个地方,您正试图连接(或添加)一个字符串和一个列表。问题出现在这里:

File "fmcrawler_sql.py", line 211, in write_fights_to_database
fightId = hash(bothFighters+fight['Event'])
    TypeError: cannot concatenate 'str' and 'list' objects

我猜bothFighters是一个列表,fight['Event']是一个字符串。将字符串更改为列表,错误可能会消失。(Python允许您一起“添加”列表,用两个列表的第一级条目创建一个较长的列表)。你知道吗

如果仍然需要帮助,请使用代码进行更新。你知道吗


List concatenation in Python

String concatenation basics if you ever need 'em.


阅读TypeError:你有一个字符串和一个列表…什么是列表是显而易见的,所以你需要把'Event'从列表中取出,并与另一个变量连接起来-然后放回一个列表中。取决于你需要什么。你知道吗

>>> ['a']+['b']
['a', 'b']
>>> l = ['b']
>>> ['a' + l[0]]
['ab']
>>> 

相关问题 更多 >