Python etl petl – read table from csv file

Python elt library petl can be used to perform extract/load – reading/writing tables from files and databases. In this tutorial we’ll read a table in csv file and remove large entries for a column.

file used for this tutorial

name,age
A,10
B,20
C,30
D,200
E,10

The etl code to remove entries where age > 100

import sys
import petl as etl

table1 = etl.fromcsv('data.csv')
table2 = table1.select(lambda rec: int(rec['age']) <= 100)
print etl.data(table2)
#etl.tocsv(table2, 'data2.csv')
sys.stdout.close()
+---+----+
| A | 10 |
+===+====+
| B | 20 |
+---+----+
| C | 30 |
+---+----+
| E | 10 |
+---+----+

Env: Python 2.7.6

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments