[英]TypeError: can only concatenate tuple (not “str”) to tuple. problem with widgets
Im trying to get information off a website into a widget, it works but i want to have each piece of text it 'finds' to be displayed on a new line.我试图将信息从网站获取到一个小部件中,它可以工作,但我想让它“找到”的每一段文本都显示在一个新的行上。 I've tried + '/n' and + ''' ''') but i get the same error.
我试过 + '/n' 和 + ''' ''') 但我得到了同样的错误。
def Supplies_scraper():
if instorechoices1.counter !=1:
url='(url)'
web_page=urlopen(url)
html_code=web_page.read().decode("UTF-8")
web_page.close()
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5],
shopping_list.delete(0.0, END)
shopping_cart.delete(0.0, END)
if products:
shopping_list.insert(0.0, products + '/n')
shopping_list.insert(0.0, '''Source: (URL)
''')
shopping_list.insert(0.0,'''--------{ Best Health Supplies }-------
''')
elif instorechoices1.counter ==1:
shopping_list.delete(0.0, END)
shopping_list.insert(END, 'Due to shortages there is only 1 of each item category per order'+ '\n')
if instorechoices1.counter != 2:
instorechoices1.counter +=1
Notice that you have trailing, in this line注意你有尾随,在这一行
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5],
which mean that after it products
is tuple
, attempt of concatenating tuple with str or str with tuple will result in这意味着在它
products
是tuple
之后,尝试将 tuple 与 str 或 str 与 tuple 连接将导致
TypeError: can only concatenate tuple (not "str") to tuple
Is that trailing ,
intentional in your code?在您的代码中是故意
,
吗? If not remove it and please check what type it is, for example by doing:如果不删除它,请检查它是什么类型,例如通过执行以下操作:
products = (findall('''data-desc =(.*?)data-price =''',html_code))[:5]
print(type(products))
If it is str
then you might concatenate it with \n
, if it is tuple
or list
you need convert it for str first, if all elements of list
or tuple
are str
s you might use .join
following way:如果它是
str
那么你可以将它与\n
连接,如果它是tuple
或list
你需要先将它转换为 str ,如果list
或tuple
的所有元素都是str
你可以使用.join
以下方式:
my_tuple = ('1','2','3')
my_list = ['1','2','3']
joined_tuple = ','.join(my_tuple)
joined_list = ','.join(my_list)
print(joined_tuple) # 1,2,3
print(joined_list) # 1,2,3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.