简体   繁体   English

TypeError:只能将元组(不是“str”)连接到元组。 小部件的问题

[英]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这意味着在它productstuple之后,尝试将 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连接,如果它是tuplelist你需要先将它转换为 str ,如果listtuple的所有元素都是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.

相关问题 获取TypeError:只能将元组(不是“ str”)连接到元组。 如何解决? - Getting TypeError: can only concatenate tuple (not “str”) to tuple. How to fix it? TypeError:只能将元组(不是“ str”)连接到元组 - TypeError: can only concatenate tuple (not “str”) to tuple TypeError:只能串联元组(不能串联“ str”) - TypeError: can only concatenate tuple (not “str”) TypeError:只能将元组(不是“str”)连接到元组错误 - TypeError: can only concatenate tuple (not “str”) to tuple Error 获取类型错误:只能通过创建日期时间将元组(不是“str”)连接到元组 - getting TypeError: can only concatenate tuple (not "str") to tuple with creating datetime plt.suptitle:TypeError:只能将元组(不是“str”)连接到元组 - plt.suptitle : TypeError: can only concatenate tuple (not “str”) to tuple TypeError:只能将元组(不是“ str”)连接到元组TROUBLES - TypeError: can only concatenate tuple (not “str”) to tuple TROUBLES Python TypeError:只能将元组(不是“ str”)连接到元组 - Python TypeError: can only concatenate tuple (not “str”) to tuple TypeError:只能将元组(不是“ str”)连接到python中的元组 - TypeError: can only concatenate tuple (not “str”) to tuple in python 解决 TypeError:只能将元组(不是“str”)连接到元组 - Resolving TypeError: can only concatenate tuple (not "str") to tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM