python iter和next使用 简单记录

文件格式:

# image/rgb/20210805/src/1628156293c9PUO9.jpg 1280 720
529 316 627 471 0.12109375 0
# image/rgb/20210805/src/1628156299YYfYJQ.jpg 1280 720
663 333 802 516 0.14296875 2
869 352 909 410 0.0453125 1
248 359 323 444 0.06640625 2
# image/rgb/20210805/src/1628156311r4yug2.jpg 1280 720
716 274 787 366 0.071875 0

 

class myIter():
    def __init__(self, file_path):
        with open(file_path, 'r')as f:
            self.lines = f.readlines()
        self.idx = 0
        self.img_len = 0
        for line in self.lines:
            if line[0] == '#':
                self.img_len += 1

    def __len__(self):
        return self.img_len
    def __iter__(self):
        return self
    def __next__(self):
        if self.idx >= len(self.lines):
            raise StopIteration
        img_name = self.lines[self.idx].split(' ')[1]
        box_list = []
        self.idx += 1
        while(self.lines[self.idx][0] != '#'):
            box_list.append(self.lines[self.idx].strip())
            self.idx += 1
            if self.idx == len(self.lines):
                return img_name, box_list
        return img_name, box_list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值