追加列表错误类型错误序列项 o:期望字符串,找到列表

发布于 2024-12-06 18:05:42 字数 926 浏览 0 评论 0原文

这是我的代码。基本上我认为它应该像这样工作 self.list 制作一个有序列表 self.contents 将列表转换为字符串,以便我可以使用 self.plbuffer.set_text(self.contents) 在可滚动窗口中显示 self.list 。然后 os.walk 遍历 top 中定义的目录,而不是 findall 获取我在 self.search 中编写的内容,找到文件名中的模式,然后将其附加到 self.list 中。

class mplay:
   def search_entry(self, widget):
    self.list = []
    self.contents = "/n".join(self.list)
    self.plbuffer.set_text(self.contents)
    search = self.search.get_text()
    top = '/home/bludiescript/tv-shows'
    for dirpath, dirnames, filenames in os.walk(top):
      for filename in filenames:
        if re.findall(filename, search):
         self.list.append(os.path.join([dirpath, filename]))

这个错误是什么意思我可以不使用 os.path.join 附加到 self.list 吗

error =   file "./mplay1.py" , line 77 in search_entry 
          self.contents = "/n".join(self.list) line 
          typeerror sequence item o: expecting string, list found 

this is my code. basicily i think it should work like this self.list makes a ordered list self.contents turns a list into a string so i can display self.list in a scrollable window with self.plbuffer.set_text(self.contents). then os.walk traverses the directory defined in top than findall takes what i write in self.search finds the pattern in filenames and then it should be appended to self.list.

class mplay:
   def search_entry(self, widget):
    self.list = []
    self.contents = "/n".join(self.list)
    self.plbuffer.set_text(self.contents)
    search = self.search.get_text()
    top = '/home/bludiescript/tv-shows'
    for dirpath, dirnames, filenames in os.walk(top):
      for filename in filenames:
        if re.findall(filename, search):
         self.list.append(os.path.join([dirpath, filename]))

what does this error mean can i not append to self.list using os.path.join

error =   file "./mplay1.py" , line 77 in search_entry 
          self.contents = "/n".join(self.list) line 
          typeerror sequence item o: expecting string, list found 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-12-13 18:05:42

该列表必须是字符串列表才能工作:

"/n".join(["123","123","234"]) # works
"/n".join([123, 123, 234]) #error, this is int

如果它是列表列表,您也会收到错误,这可能是您的情况:

"/n".join([[123, 123, 234],[123, 123, 234]]) # error

放入 print self.list 看看它是什么样子。

当你说它在其他地方运行良好时,可能是因为列表的内容不同。

另请注意,加入空列表 [] 将返回空字符串,因此该行实际上不执行任何操作。

The list must be a list of strings for it to work:

"/n".join(["123","123","234"]) # works
"/n".join([123, 123, 234]) #error, this is int

You also get an error if it is a list of lists, which is probably the case in yours:

"/n".join([[123, 123, 234],[123, 123, 234]]) # error

Throw in a print self.list to see what it looks like.

When you say it runs fine elsewhere, probably that is because the content of the list is different.

Also, note that joining an empty list [] will return an empty string, so that line is effectively doing nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文