Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

Additional methods for the petl library that enable reading fixed-width files

Project description

# petl_fwf
This package contains two additional methods which add the ability to read fixed-width files to the fabulous [petl package](https://github.com/petl-developers/petl).

## fromfwf
This is the primary method which this package adds. Usage is straightforward, and similar to other methods in the petl library

```sh
>>> import fromfwf
>>> # setup a sample file
... text = ' 18 5 2\\n2018 5 2\\n20180502'
>>> with open('example.txt', 'w') as f:
... f.write(text)
...
28
>>> table1 = etl.fromfwf('example.txt', widths=[4, 2, 2])
>>> table1
+--------+------+------+
| lines | | |
+========+======+======+
| ' 18' | ' 5' | ' 2' |
+--------+------+------+
| '2018' | ' 5' | ' 2' |
+--------+------+------+
| '2018' | '05' | '02' |
+--------+------+------+
>>> # Specify headers for the file
... table1 = etl.fromfwf('example.txt', widths=[4, 2, 2], header=['year', 'month', 'day'])
>>> table1
+--------+-------+------+
| year | month | day |
+========+=======+======+
| ' 18' | ' 5' | ' 2' |
+--------+-------+------+
| '2018' | ' 5' | ' 2' |
+--------+-------+------+
| '2018' | '05' | '02' |
+--------+-------+------+
```

## skiplast
This method will skip the last n rows of a table.
```sh
>>> import petl as etl
>>> table1 = [['foo', 'bar'],
... ['a', 1],
... ['b', 2],
... ['c', 3],
... ['FOOTER ROW']]
>>> table2 = etl.skiplast(table1, 1)
>>> table2
+-----+-----+
| foo | bar |
+=====+=====+
| 'a' | 1 |
+-----+-----+
| 'b' | 2 |
+-----+-----+
| 'c' | 3 |
+-----+-----+
```

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
petl_fwf-0.0.2.tar.gz (5.3 kB) Copy SHA256 hash SHA256 Source None Aug 14, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page