# View a sequence of columns as a table:
import petl as etl
cols = [[0, 1, 2], ['a', 'b', 'c']]
tbl = etl.fromcolumns(cols)
tbl
| f0 | f1 |
|---|---|
| 0 | a |
| 1 | b |
| 2 | c |
# If columns are not the same length, values will be padded to the length of the longest column with missing, which is None by default:
cols = [[0, 1, 2], ['a', 'b']]
tbl = etl.fromcolumns(cols, missing='NA')
tbl
| f0 | f1 |
|---|---|
| 0 | a |
| 1 | b |
| 2 | NA |
# petl.io.csv.fromcsv(source=None, encoding=None, errors='strict', header=None, **csvargs)
# Extract a table from a delimited file:
import csv
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 2]]
with open('example.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(table1)
table2 = etl.fromcsv('example.csv'); table2
| foo | bar |
|---|---|
| a | 1 |
| b | 2 |
| c | 2 |
# The “to…” functions load data from a table into a file-like source or database.
# For functions that accept a source argument, if the source argument is None or a string it is interpreted as follows:
# None - write to stdout
# string ending with .gz or .bgz - write to file via gzip decompression
# string ending with .bz2 - write to file via bz2 decompression
# any other string - write directly to file
# Write the table to a CSV file:
etl.tocsv(table1, 'example.csv')
print(open('example.csv').read())
foo,bar a,1 b,2 c,2
table1 = etl.fromcolumns(table1); table1
| f0 | f1 | f2 | f3 |
|---|---|---|---|
| foo | a | b | c |
| bar | 1 | 2 | 2 |
tbl2 = etl.fromcolumns(cols)
# Extract a table from data pickled in the given file. The rows in the table should have been pickled to the file one at a time:
import pickle
with open('example.p', 'wb') as f:
pickle.dump(['foo', 'bar'], f)
pickle.dump(['a', 1], f)
pickle.dump(['b', 2], f)
pickle.dump(['c', 2.5], f)
table1 = etl.frompickle('example.p')
table1
| foo | bar |
|---|---|
| a | 1 |
| b | 2 |
| c | 2.5 |
# Write the table to a pickle file:
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 2]]
etl.topickle(table1, 'example.p')
table2 = etl.frompickle('example.p')
table2
| foo | bar |
|---|---|
| a | 1 |
| b | 2 |
| c | 2 |
# Extract a table from lines in the given text file:
import petl as etl
text = 'a,1\nb,2\nc,2\n'
with open('example.txt', 'w') as f:
f.write(text)
table1 = etl.fromtext('example.txt'); table1
| lines |
|---|
| a,1 |
| b,2 |
| c,2 |
# post-process, e.g., with capture()
table2 = table1.capture('lines', '(.*),(.*)$', ['foo', 'bar']); table2
| foo | bar |
|---|---|
| a | 1 |
| b | 2 |
| c | 2 |
# Write the table as HTML to a file:
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 2]]
etl.tohtml(table1, 'example.html', caption='example table')
print(open('example.html').read())
<table class='petl'> <caption>example table</caption> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> <tr> <td>c</td> <td style='text-align: right'>2</td> </tr> </tbody> </table>
import petl as etl
import pymysql
import sqlite3
connection = sqlite3.connect('pytest')
table = etl.fromdb(connection, 'SELECT * FROM example')