Python excel文件读取

来源:互联网 发布:图搜索算法的特点 编辑:程序博客网 时间:2024/04/30 20:14

1. 读取excel文件,建类存储excel数据,

在我的excel文件中存储了学生信息,(姓名,出生年月)

定义class Student

class Etudiant(object):
def __init__(self,idEtudiant, nom,prenom,date,group):
self.idEtudiant=idEtudiant
self.nom = nom
self.prenom=prenom
self.date=date
self.group=group


def adresselec(self):
return self.prenom + '.' + self.nom +'@etu.univ-tours.fr'

def age(self):
return 2016-self.date.year

定义两个函数产生邮箱和年龄

定义日期类

class Date(object):
"""docstring for Date"""
def __init__(self, day,month,year):

self.day=day
self.month=month
self.year=year
def __eq__(self,date):
if self.year == date.year and self.month==date.month and self.day==date.day:
return 1
else:
return 0;

def __lt__(self,date):
if self.year <date.year:
return 1
elif self.year>date.year:
return 0
elif self.month< date.month:
return 1
elif self.month>date.month:
return 0
elif self.day < date.day:
return 1
else: return 0

重写==和<两个函数。

读取excel文件

from student import Etudiant
from date import Date
import csv


students=[]
reader=csv.reader(open('fichetu.csv','r'))
for line in reader:
     numero, nom,prenom,date=line[0].split(';')
     day, month,year =date.split('/')
     dat=Date(day,month,year)
     student=Etudiant(nom,prenom,dat)
     students.append(student)

文件内容被存储到了students这个list里边OK!


0 0
原创粉丝点击