简体   繁体   English

Python如何获取类字节列表的长度

[英]Python how do I get the length of a class byte list

I am using email IMAP4, and I get the number of emails with a given subject like below list, I also get it's type. 我正在使用电子邮件IMAP4,我得到了给定主题的电子邮件数量,如下面的列表所示,我也得到了它的类型。 My goal is to get the size or length of it: 我的目标是获取尺寸或长度:

  emailsUIDs = [b'80 83 84 85 86 87 88 89 90 91 93 126 136 137 138 139 140']
  print(type(emailsUIDs ))  # Prints --> type of Object =  <class 'list'>

  myLen = len((str(emailsUIDs ).replace("b'", "").replace("'", "")).split())

I tried len(emailsUIDs ) but I only get 1, is there a better way to get the length of emailsUIDs and not have to do string manipulation as I am doing 我尝试了len(emailsUIDs)但我只得到1,是否有更好的方法来获取emailsUIDs的长度,而不必像我正在做的那样进行字符串操作

You have a list with one element (a bytes object). 您有一个包含一个元素的列表(一个字节对象)。 You need to do this to extract the list of ids: 您需要执行以下操作以提取ID列表:

emailsUIDs = [b'80 83 84 85 86 87 88 89 90 91 93 126 136 137 138 139 140']
ids_bytes = emailsUIDs[0]
ids_bytes_list = ids_bytes.split()
ids_list = map(int, ids_bytes_list)

then you can use the id's in numerical form from that list, or if you just needed the count you could have simply done len(ids_bytes_list) straight. 那么您可以使用该列表中数字形式的ID,或者如果您只需要计数,则可以直接完成len(ids_bytes_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM