1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
from __future__ import division, print_function, absolute_import
# fromtext()
############
import petl as etl
# setup example file
text = 'a,1\nb,2\nc,2\n'
with open('example.txt', 'w') as f:
f.write(text)
table1 = etl.fromtext('example.txt')
table1
# post-process, e.g., with capture()
table2 = table1.capture('lines', '(.*),(.*)$', ['foo', 'bar'])
table2
# totext()
##########
import petl as etl
table1 = [['foo', 'bar'],
['a', 1],
['b', 2],
['c', 2]]
prologue = '''{| class="wikitable"
|-
! foo
! bar
'''
template = '''|-
| {foo}
| {bar}
'''
epilogue = '|}'
etl.totext(table1, 'example.txt', template, prologue, epilogue)
# see what we did
print(open('example.txt').read())
|