How to use the petl.convert function in petl

To help you get started, we’ve selected a few petl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SolutionGuidance / psm / etl / leie / leie / etl.py View on Github external
def clean(table):
    """Do some cleanup of TABLE

    TABLE is a petl table."""

    # Rename column to expand name
    table = etl.rename(table, {'WVRSTATE': 'waiverstate'})

    # More conversions
    table = etl.convert(table, {
        'EXCLTYPE': lambda f: f.strip(), # Trim extra spaces
        'EXCLDATE': munge_date, # Arrange date for sqlite
        'REINDATE': munge_date, # Arrange date for sqlite
        'WAIVERDATE': munge_date # Arrange date for sqlite
    })

    # Do some cleanup conversions on individual data
    table = etl.convert(table, {'DOB': munge_date,
                                'MIDNAME': lambda f: f if f != " " else ""  # no spaces as middle names
    })
    table = etl.convertall(
            table,
            lambda f: None if f.strip() == '' else f
    )
    return table
github petl-developers / petl / examples / transform / conversions.py View on Github external
['C', '1.2', 56]]
# using a built-in function:
table2 = etl.convert(table1, 'bar', float)
table2
# using a lambda function::
table3 = etl.convert(table1, 'baz', lambda v: v*2)
table3
# a method of the data value can also be invoked by passing
# the method name
table4 = etl.convert(table1, 'foo', 'lower')
table4
# arguments to the method invocation can also be given
table5 = etl.convert(table1, 'foo', 'replace', 'A', 'AA')
table5
# values can also be translated via a dictionary
table7 = etl.convert(table1, 'foo', {'A': 'Z', 'B': 'Y'})
table7
# the same conversion can be applied to multiple fields
table8 = etl.convert(table1, ('foo', 'bar', 'baz'), str)
table8
# 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
github petl-developers / petl / examples / transform / conversions.py View on Github external
from __future__ import division, print_function, absolute_import


# convert()
###########

import petl as etl
table1 = [['foo', 'bar', 'baz'],
          ['A', '2.4', 12],
          ['B', '5.7', 34],
          ['C', '1.2', 56]]
# using a built-in function:
table2 = etl.convert(table1, 'bar', float)
table2
# using a lambda function::
table3 = etl.convert(table1, 'baz', lambda v: v*2)
table3
# a method of the data value can also be invoked by passing
# the method name
table4 = etl.convert(table1, 'foo', 'lower')
table4
# arguments to the method invocation can also be given
table5 = etl.convert(table1, 'foo', 'replace', 'A', 'AA')
table5
# values can also be translated via a dictionary
table7 = etl.convert(table1, 'foo', {'A': 'Z', 'B': 'Y'})
table7
# the same conversion can be applied to multiple fields
table8 = etl.convert(table1, ('foo', 'bar', 'baz'), str)
table8
# multiple conversions can be specified at the same time
table9 = etl.convert(table1, {'foo': 'lower',
github petl-developers / petl / src / petl / examples.py View on Github external
['A', '2.4'],
          ['B', '5.7'],
          ['C', '1.2'],
          ['D', '8.3']]
table6 = [['gender', 'age'],
          ['M', 12],
          ['F', 34],
          ['-', 56]]

from petl import convert, look
look(table1)
# using the built-in float function:
table2 = convert(table1, 'bar', float)
look(table2)
# using a lambda function::
table3 = convert(table2, 'bar', lambda v: v**2)
look(table3)    
# a method of the data value can also be invoked by passing the method name
table4 = convert(table1, 'foo', 'lower')
look(table4)
# arguments to the method invocation can also be given
table5 = convert(table4, 'foo', 'replace', 'a', 'aa')
look(table5)
# values can also be translated via a dictionary
look(table6)
table7 = convert(table6, 'gender', {'M': 'male', 'F': 'female'})
look(table7)


# convertnumbers

table1 = [['foo', 'bar', 'baz', 'quux'],
github petl-developers / petl / examples / transform / conversions.py View on Github external
###########

import petl as etl
table1 = [['foo', 'bar', 'baz'],
          ['A', '2.4', 12],
          ['B', '5.7', 34],
          ['C', '1.2', 56]]
# using a built-in function:
table2 = etl.convert(table1, 'bar', float)
table2
# using a lambda function::
table3 = etl.convert(table1, 'baz', lambda v: v*2)
table3
# a method of the data value can also be invoked by passing
# the method name
table4 = etl.convert(table1, 'foo', 'lower')
table4
# arguments to the method invocation can also be given
table5 = etl.convert(table1, 'foo', 'replace', 'A', 'AA')
table5
# values can also be translated via a dictionary
table7 = etl.convert(table1, 'foo', {'A': 'Z', 'B': 'Y'})
table7
# the same conversion can be applied to multiple fields
table8 = etl.convert(table1, ('foo', 'bar', 'baz'), str)
table8
# 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
github petl-developers / petl / src / petl / examples.py View on Github external
['F', 34],
          ['-', 56]]

from petl import convert, look
look(table1)
# using the built-in float function:
table2 = convert(table1, 'bar', float)
look(table2)
# using a lambda function::
table3 = convert(table2, 'bar', lambda v: v**2)
look(table3)    
# a method of the data value can also be invoked by passing the method name
table4 = convert(table1, 'foo', 'lower')
look(table4)
# arguments to the method invocation can also be given
table5 = convert(table4, 'foo', 'replace', 'a', 'aa')
look(table5)
# values can also be translated via a dictionary
look(table6)
table7 = convert(table6, 'gender', {'M': 'male', 'F': 'female'})
look(table7)


# 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)
github petl-developers / petl / examples / transform / conversions.py View on Github external
table3
# a method of the data value can also be invoked by passing
# the method name
table4 = etl.convert(table1, 'foo', 'lower')
table4
# arguments to the method invocation can also be given
table5 = etl.convert(table1, 'foo', 'replace', 'A', 'AA')
table5
# values can also be translated via a dictionary
table7 = etl.convert(table1, 'foo', {'A': 'Z', 'B': 'Y'})
table7
# the same conversion can be applied to multiple fields
table8 = etl.convert(table1, ('foo', 'bar', 'baz'), str)
table8
# 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
github petl-developers / petl / examples / intro.py View on Github external
from __future__ import division, print_function, absolute_import

example_data = """foo,bar,baz
a,1,3.4
b,2,7.4
c,6,2.2
d,9,8.1
"""
with open('example.csv', 'w') as f:
    f.write(example_data)

import petl as etl
table1 = etl.fromcsv('example.csv')
table2 = etl.convert(table1, 'foo', 'upper')
table3 = etl.convert(table2, 'bar', int)
table4 = etl.convert(table3, 'baz', float)
table5 = etl.addfield(table4, 'quux', lambda row: row.bar * row.baz)
table5

table = (
    etl
    .fromcsv('example.csv')
    .convert('foo', 'upper')
    .convert('bar', int)
    .convert('baz', float)
    .addfield('quux', lambda row: row.bar * row.baz)
)
table

l = [['foo', 'bar'], ['a', 1], ['b', 2], ['c', 2]]
table = etl.wrap(l)
github SolutionGuidance / psm / etl / leie / leie / etl.py View on Github external
TABLE is a petl table."""

    # Rename column to expand name
    table = etl.rename(table, {'WVRSTATE': 'waiverstate'})

    # More conversions
    table = etl.convert(table, {
        'EXCLTYPE': lambda f: f.strip(), # Trim extra spaces
        'EXCLDATE': munge_date, # Arrange date for sqlite
        'REINDATE': munge_date, # Arrange date for sqlite
        'WAIVERDATE': munge_date # Arrange date for sqlite
    })

    # Do some cleanup conversions on individual data
    table = etl.convert(table, {'DOB': munge_date,
                                'MIDNAME': lambda f: f if f != " " else ""  # no spaces as middle names
    })
    table = etl.convertall(
            table,
            lambda f: None if f.strip() == '' else f
    )
    return table