CSV iterator
This commit is contained in:
parent
6e8a668a6a
commit
7cd2169829
1 changed files with 43 additions and 0 deletions
43
csv.py
Normal file
43
csv.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
|
||||||
|
class CSVFile(object):
|
||||||
|
def __init__(self, filename):
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return CSVFileIterator(open(self.filename,"r"))
|
||||||
|
|
||||||
|
class CSVFileIterator(object):
|
||||||
|
def __init__(self, fh):
|
||||||
|
self.fh = fh
|
||||||
|
self.lines = iter(fh.readlines())
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
return CSVEntry(next(self.lines))
|
||||||
|
|
||||||
|
class CSVEntry(object):
|
||||||
|
def __init__(self,line):
|
||||||
|
self.data = line.split(";")
|
||||||
|
|
||||||
|
def getInt(self, index):
|
||||||
|
return int(self.data[index])
|
||||||
|
|
||||||
|
def getFloat(self, index):
|
||||||
|
return float(self.data[index])
|
||||||
|
|
||||||
|
def getString(self, index):
|
||||||
|
return self.data[index]
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self.getString(index)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.data)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.data)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from sys import argv
|
||||||
|
for entry in CSVFile(argv[1]):
|
||||||
|
for field in entry:
|
||||||
|
print("%10s" % field, end="")
|
Loading…
Reference in a new issue