Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('=' * len(repr(dbo)))
print('EXERCISE UNICODE')
print(repr(dbo))
print('=' * len(repr(dbo)))
print()
expect = ((u'name', u'id'),
(u'Արամ Խաչատրյան', 1),
(u'Johann Strauß', 2),
(u'Вагиф Сәмәдоғлу', 3),
(u'章子怡', 4),
)
actual = etl.fromdb(dbo, 'SELECT * FROM test_unicode')
print('write some data and verify...')
etl.todb(expect, dbo, 'test_unicode')
ieq(expect, actual)
print(etl.look(actual))
def process_rows(resource_):
for row_index, row in enumerate(resource_):
if pass_row_index:
parameters.update(row_index=row_index)
new_row = row_processor(row, **parameters)
yield new_row
if verbose and row_index < LOG_SAMPLE_SIZE:
sample_rows.append(new_row)
if verbose:
table = look(fromdicts(sample_rows), limit=LOG_SAMPLE_SIZE)
message = 'Output of processor %s for resource %s is...\n%s'
args = row_processor.__name__, resource_index, table
logging.info(message, *args)
['M', 12],
['F', 34],
['-', 56]]
from petl import extend, look
look(table1)
# using a fixed value
table2 = extend(table1, 'baz', 42)
look(table2)
# calculating the value
table2 = extend(table1, 'baz', lambda rec: rec['bar'] * 2)
look(table2)
# an expression string can also be used via expr
from petl import expr
table3 = extend(table1, 'baz', expr('{bar} * 2'))
look(table3)
# rowslice
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42]]
from petl import rowslice, look
look(table1)
table2 = rowslice(table1, 2)
look(table2)
table3 = rowslice(table1, 1, 4)
#!/usr/bin/env python
"""
TODO doc me
"""
from petl import cat, look
table1 = [['foo', 'bar'],
[1, 'A'],
[2, 'B']]
table2 = [['bar', 'baz'],
['C', True],
['D', False]]
table3 = cat(table1, table2, missing=None)
look(table3)
look(table2)
# tocsv
table = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 2]]
from petl import tocsv, look
look(table)
tocsv(table, 'test.csv', delimiter='\t')
# look what it did
from petl import fromcsv
look(fromcsv('test.csv', delimiter='\t'))
# appendcsv
table = [['foo', 'bar'],
['d', 7],
['e', 42],
['f', 12]]
# look at an existing CSV file
from petl import look, fromcsv
testcsv = fromcsv('test.csv', delimiter='\t')
look(testcsv)
# append some data
look(table)
from petl import appendcsv
# leftjoin
table1 = [['id', 'colour'],
[1, 'blue'],
[2, 'red'],
[3, 'purple']]
table2 = [['id', 'shape'],
[1, 'circle'],
[3, 'square'],
[4, 'ellipse']]
from petl import leftjoin, look
look(table1)
look(table2)
table3 = leftjoin(table1, table2, key='id')
look(table3)
# rightjoin
table1 = [['id', 'colour'],
[1, 'blue'],
[2, 'red'],
[3, 'purple']]
table2 = [['id', 'shape'],
[1, 'circle'],
[3, 'square'],
[4, 'ellipse']]
from petl import rightjoin, look
['B', 2]]
table7 = [['bar', 'foo'],
['A', 1],
['B', 2]]
table8 = [['bar', 'baz'],
['C', True],
['D', False]]
from petl import look, cat
look(table1)
look(table2)
table3 = cat(table1, table2)
look(table3)
# can also be used to square up a single table with uneven rows
look(table4)
look(cat(table4))
# use the header keyword argument to specify a fixed set of fields
look(table5)
table6 = cat(table5, header=['A', 'foo', 'B', 'bar', 'C'])
look(table6)
# using the header keyword argument with two input tables
look(table7)
look(table8)
table9 = cat(table7, table8, header=['A', 'foo', 'B', 'bar', 'C'])
look(table9)
# convert
table1 = [['foo', 'bar'],
['A', '2.4'],
['B', '5.7'],
look(fromsqlite3('test.db', 'select * from foobar'))
# appendsqlite3
moredata = [['foo', 'bar'],
['d', 7],
['e', 9],
['f', 1]]
from petl import appendsqlite3, look
look(moredata)
appendsqlite3(moredata, 'test.db', 'foobar')
# look what it did
from petl import look, fromsqlite3
look(fromsqlite3('test.db', 'select * from foobar'))
# mergesort
table1 = (('foo', 'bar'),
('A', 6),
('C', 2),
('D', 10),
('A', 9),
('F', 1))
table2 = (('foo', 'bar'),
('B', 3),
('D', 10),
('A', 10),
('F', 4))
# rowslice
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42]]
from petl import rowslice, look
look(table1)
table2 = rowslice(table1, 2)
look(table2)
table3 = rowslice(table1, 1, 4)
look(table3)
table4 = rowslice(table1, 0, 5, 2)
look(table4)
# head
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 5],
['d', 7],
['f', 42],
['f', 3],
['h', 90]]
from petl import head, look
table3 = [['id', 'time', 'height', 'weight'],
[1, 11, 66.4, 12.2],
[2, 16, 53.2, 17.3],
[3, 12, 34.5, 9.4]]
from petl import melt, look
look(table1)
table2 = melt(table1, 'id')
look(table2)
# compound keys are supported
look(table3)
table4 = melt(table3, key=['id', 'time'])
look(table4)
# a subset of variable fields can be selected
table5 = melt(table3, key=['id', 'time'], variables=['height'])
look(table5)
# recast
table1 = [['id', 'variable', 'value'],
[3, 'age', 16],
[1, 'gender', 'F'],
[2, 'gender', 'M'],
[2, 'age', 17],
[1, 'age', 12],
[3, 'gender', 'M']]
table3 = [['id', 'vars', 'vals'],
[3, 'age', 16],
[1, 'gender', 'F'],
[2, 'gender', 'M'],
[2, 'age', 17],