class UserInfo(object):
'Class to restore UserInformation'
def __init__ (self):
self.attrilist={}
self.__attributes=[]
def updateAttributes(self,attributes):
self.__attributes=attributes
def updatePairs(self,values):
for i in range(len(values)):
self.attrilist[self.__attributes[i]]=values[i]
def fib(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b n = n + 1 return 'done'
>>> fib(6) 1 1 2 3 5 8 'done'
def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1
>>> f = fib(6) >>> f <generator object fib at 0x104feaaa0> >>> for i in f: ... print(i) ... 1 1 2 3 5 8 >>>
def ObjectGenerator(maxlinenum):
filename='/home/thinkit/Documents/usr_info/USER.csv'
attributes=[]
linenum=1
a=UserInfo()
file=open(filename)
while linenum < maxlinenum:
values=[]
line=str.decode(file.readline(),'gb2312')#linecache.getline(filename, linenum,'gb2312')
if line=='':
print'reading fail! Please check filename!'
break
str_list=line.split(',')
for item in str_list:
item=item.strip()
item=item.strip('"')
item=item.strip(''')
item=item.strip('+0*')
item=catchTime(item)
if linenum==1:
attributes.append(item)
else:
values.append(item)
if linenum==1:
a.updateAttributes(attributes)
else:
a.updatePairs(values)
yield a.attrilist #change to ' a ' to use
linenum = linenum +1
item=item.strip()#除去字符串前后的所有转义字符,如t,n等
item=item.strip('"')#除去前后的"
item=item.strip(''')
item=item.strip('+0*')#除去前后的+00...00,*表示0的个数可以任意多,也可以没有
re.match(pattern, string, flags=0)
import time
import re
def catchTime(item):
# check if it's time
matchObj=re.match(r'd{4}-d{2}-d{2}',item, flags= 0)
if matchObj!= None :
item =time.strptime(item,'%Y-%m-%d')
#print "returned time: %s " %item
return item
else:
matchObj=re.match(r'd{4}/d{2}/d{2}sd+:d+:d+',item,flags=0 )
if matchObj!= None :
item =time.strptime(item,'%Y/%m/%d %H:%M:%S')
#print "returned time: %s " %item
return item
import collections
import time
import re
class UserInfo(object):
'Class to restore UserInformation'
def __init__ (self):
self.attrilist=collections.OrderedDict()# ordered
self.__attributes=[]
def updateAttributes(self,attributes):
self.__attributes=attributes
def updatePairs(self,values):
for i in range(len(values)):
self.attrilist[self.__attributes[i]]=values[i]
def catchTime(item):
# check if it's time
matchObj=re.match(r'd{4}-d{2}-d{2}',item, flags= 0)
if matchObj!= None :
item =time.strptime(item,'%Y-%m-%d')
#print "returned time: %s " %item
return item
else:
matchObj=re.match(r'd{4}/d{2}/d{2}sd+:d+:d+',item,flags=0 )
if matchObj!= None :
item =time.strptime(item,'%Y/%m/%d %H:%M:%S')
#print "returned time: %s " %item
return item
def ObjectGenerator(maxlinenum):
filename='/home/thinkit/Documents/usr_info/USER.csv'
attributes=[]
linenum=1
a=UserInfo()
file=open(filename)
while linenum < maxlinenum:
values=[]
line=str.decode(file.readline(),'gb2312')#linecache.getline(filename, linenum,'gb2312')
if line=='':
print'reading fail! Please check filename!'
break
str_list=line.split(',')
for item in str_list:
item=item.strip()
item=item.strip('"')
item=item.strip(''')
item=item.strip('+0*')
item=catchTime(item)
if linenum==1:
attributes.append(item)
else:
values.append(item)
if linenum==1:
a.updateAttributes(attributes)
else:
a.updatePairs(values)
yield a.attrilist #change to ' a ' to use
linenum = linenum +1
if __name__ == '__main__':
for n in ObjectGenerator(10):
print n #输出字典,看是否正确
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有