def generate(): #columns = petl.util.base.header(table) data = petl.convertnumbers(table) for row in petl.util.base.dicts(data): #Process blades here for b in blades['post']: row = b.run(row, meta) yield json.dumps(row)+'\n'
table11 = convert(table1) table11['foo'] = 'lower' table11['bar'] = float table11['baz'] = lambda v: v*2 look(table11) # convertnumbers table1 = [['foo', 'bar', 'baz', 'quux'], ['1', '3.0', '9+3j', 'aaa'], ['2', '1.3', '7+2j', None]] from petl import convertnumbers, look look(table1) table2 = convertnumbers(table1) look(table2) # addfield table1 = [['foo', 'bar'], ['M', 12], ['F', 34], ['-', 56]] from petl import addfield, look look(table1) # using a fixed value table2 = addfield(table1, 'baz', 42) look(table2)
# multiple conversions can be specified at the same time table9 = etl.convert(table1, {'foo': 'lower', 'bar': float, 'baz': lambda v: v * 2}) table9 # ...or alternatively via a list table10 = etl.convert(table1, ['lower', float, lambda v: v*2]) table10 # conversion can be conditional table11 = etl.convert(table1, 'baz', lambda v: v * 2, where=lambda r: r.foo == 'B') table11 # conversion can access other values from the same row table12 = etl.convert(table1, 'baz', lambda v, row: v * float(row.bar), pass_row=True) table12 # convertnumbers() ################## import petl as etl table1 = [['foo', 'bar', 'baz', 'quux'], ['1', '3.0', '9+3j', 'aaa'], ['2', '1.3', '7+2j', None]] table2 = etl.convertnumbers(table1) table2
import petl as etl table_header = [ "Fixed Acidity", "Volatile Acidity", "Citric Acid", "Sugar", "Chlorides", "Free SO2", "Total SO2", "Density", "pH", "Sulfates", "Alcohol", "Quality" ] table1 = etl.addfield( etl.convertnumbers( etl.setheader(etl.fromcsv('winequality-red.csv'), table_header)), "Type", "Red") table2 = etl.addfield( etl.convertnumbers( etl.setheader(etl.fromcsv('winequality-white.csv'), table_header)), "Type", "White") #print(etl.head(table1)) #print(etl.head(table2)) table1_filtered = etl.select(table1, "Quality", lambda v: v > 6) table2_filtered = etl.select(table2, "Quality", lambda v: v > 4) good_wines = etl.cat(table1_filtered, table2_filtered) good_wines_enhanced = etl.addfields( good_wines, [("Max Acidity", lambda rec: rec["Fixed Acidity"] + rec["Volatile Acidity"]), ("Locked SO2", lambda rec: rec["Total SO2"] - rec["Free SO2"])]) #print(etl.head(good_wines_enhanced)) #print(etl.tail(good_wines_enhanced))