Python 自定义iterator生成器

#计数版
class countdown(object):
	def __init__(self,start):
		self.start = start
	def __iter__(self):
		return countdown_iter(self.start)

class countdown_iter(object):
	def __init__(self, count):
		self.count = count
	def __next__(self):
		if self.count <= 0:
			raise StopIteration
		r = self.count
		self.count -= 1
		return r

  

#pandas读取多个文件
import pandas as pd

class read_csv_paths(object):
	def __init__(self,paths):
		self.paths = paths
	def __iter__(self):
		return read_csv_iter(self.paths)

class read_csv_iter(object):
	def __init__(self, paths):
		self.paths = paths
	def __next__(self):
		if len(self.paths)<=0:
			raise StopIteration
		path=self.paths.pop()
		#print(path)
		df=pd.read_csv(path,low_memory=False,dtype='object')
		return df

  

猜你喜欢

转载自www.cnblogs.com/mahailuo/p/10207620.html
今日推荐