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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division
# standard library dependencies
import io
from petl.compat import next, PY2, text_type
# internal dependencies
from petl.util.base import Table, asdict
from petl.io.base import getcodec
from petl.io.sources import read_source_from_arg, write_source_from_arg
def fromtext(source=None, encoding=None, errors='strict', strip=None,
header=('lines',)):
"""
Extract a table from lines in the given text file. E.g.::
>>> 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)
...
12
>>> 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' |
+-----+-----+
Note that the strip() function is called on each line, which by default
will remove leading and trailing whitespace, including the end-of-line
character - use the `strip` keyword argument to specify alternative
characters to strip. Set the `strip` argument to `False` to disable this
behaviour and leave line endings in place.
"""
source = read_source_from_arg(source)
return TextView(source, header=header, encoding=encoding,
errors=errors, strip=strip)
class TextView(Table):
def __init__(self, source, header=('lines',), encoding=None,
errors='strict', strip=None):
self.source = source
self.header = header
self.encoding = encoding
self.errors = errors
self.strip = strip
def __iter__(self):
with self.source.open('rb') as buf:
# deal with text encoding
if PY2:
codec = getcodec(self.encoding)
f = codec.streamreader(buf, errors=self.errors)
else:
f = io.TextIOWrapper(buf,
encoding=self.encoding,
errors=self.errors,
newline='')
# generate the table
try:
if self.header is not None:
yield tuple(self.header)
if self.strip is False:
for line in f:
yield (line,)
else:
for line in f:
yield (line.strip(self.strip),)
finally:
if not PY2:
f.detach()
def totext(table, source=None, encoding=None, errors='strict', template=None,
prologue=None, epilogue=None):
"""
Write the table to a text file. E.g.::
>>> 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=template,
... prologue=prologue, epilogue=epilogue)
>>> # see what we did
... print(open('example.txt').read())
{| class="wikitable"
|-
! foo
! bar
|-
| a
| 1
|-
| b
| 2
|-
| c
| 2
|}
The `template` will be used to format each row via
`str.format <http://docs.python.org/library/stdtypes.html#str.format>`_.
"""
_writetext(table, source=source, mode='wb', encoding=encoding,
errors=errors, template=template, prologue=prologue,
epilogue=epilogue)
Table.totext = totext
def appendtext(table, source=None, encoding=None, errors='strict', template=None,
prologue=None, epilogue=None):
"""
Append the table to a text file.
"""
_writetext(table, source=source, mode='ab', encoding=encoding,
errors=errors, template=template, prologue=prologue,
epilogue=epilogue)
Table.appendtext = appendtext
def _writetext(table, source, mode, encoding, errors, template, prologue,
epilogue):
# guard conditions
assert template is not None, 'template is required'
# prepare source
source = write_source_from_arg(source, mode)
with source.open(mode) as buf:
# deal with text encoding
if PY2:
codec = getcodec(encoding)
f = codec.streamwriter(buf, errors=errors)
else:
f = io.TextIOWrapper(buf,
encoding=encoding,
errors=errors,
newline='')
# write the table
try:
if prologue is not None:
f.write(prologue)
it = iter(table)
try:
hdr = next(it)
except StopIteration:
hdr = []
flds = list(map(text_type, hdr))
for row in it:
rec = asdict(flds, row)
s = template.format(**rec)
f.write(s)
if epilogue is not None:
f.write(epilogue)
f.flush()
finally:
if not PY2:
f.detach()
def teetext(table, source=None, encoding=None, errors='strict', template=None,
prologue=None, epilogue=None):
"""
Return a table that writes rows to a text file as they are iterated over.
"""
assert template is not None, 'template is required'
return TeeTextView(table, source=source, encoding=encoding, errors=errors,
template=template, prologue=prologue, epilogue=epilogue)
Table.teetext = teetext
class TeeTextView(Table):
def __init__(self, table, source=None, encoding=None, errors='strict',
template=None, prologue=None, epilogue=None):
self.table = table
self.source = source
self.encoding = encoding
self.errors = errors
self.template = template
self.prologue = prologue
self.epilogue = epilogue
def __iter__(self):
return _iterteetext(self.table, self.source, self.encoding,
self.errors, self.template, self.prologue,
self.epilogue)
def _iterteetext(table, source, encoding, errors, template, prologue, epilogue):
# guard conditions
assert template is not None, 'template is required'
# prepare source
source = write_source_from_arg(source)
with source.open('wb') as buf:
# deal with text encoding
if PY2:
codec = getcodec(encoding)
f = codec.streamwriter(buf, errors=errors)
else:
f = io.TextIOWrapper(buf,
encoding=encoding,
errors=errors)
# write the data
try:
if prologue is not None:
f.write(prologue)
it = iter(table)
try:
hdr = next(it)
except StopIteration:
return
yield tuple(hdr)
flds = list(map(text_type, hdr))
for row in it:
rec = asdict(flds, row)
s = template.format(**rec)
f.write(s)
yield row
if epilogue is not None:
f.write(epilogue)
f.flush()
finally:
if not PY2:
f.detach()
|