Python Fixed Width



Latest version

(2 replies) I'm writing a script to take a tab-delimited input file where the format is known and transform it to a fixed-width text file. I thought that the script might be easier to troubleshoot or edit later (if the file formats change, for example) if I specified the column names for the input and output files, which is why I didn't just map input column 1 - export column 14.

  • Python string formatting fixed width. Setting fixed length with python. Python python-3.x python-2.7 format. Share improve this question follow edited Oct 27 at.
  • Python string method zfill pads string on the left with zeros to fill width. Following is the syntax for zfill method −. Str.zfill(width) Parameters. Width − This is final width of the string. This is the width which we would get after filling zeros.
  • Easy two-way conversion between Python dictionaries and fixed-width files. The FixedWidth class has been used in production without modification for several years. This module has also proven useful for 'debugging' a fixed-width spec - an invalid configuration reports an error that may not be obvious from reading the spec document.

Released:

simple fixed-width integers

Project description

This module provides fixed-size integer classes which retain their fixed nature acrossarithmetic operations. It is geared towards users who need to emulate machine integers.

It provides flexible classes for defining integers with a fixed number of bits, as wellas predefined classes for common machine integer sizes. These classes can be used asdrop-in replacements for int/long, and can be sliced to extract bitfields.

Mutable versions of these integers are provided, enabling usages such as emulation ofmachine registers.

Basic Usage

A collection of predefined fixed-width integers for widths 8, 16, 32 and 64 are availablein signed and unsigned varieties. Mutable and immutable versions of each type are provided.

These are named as [Mutable][U]Int<N>, e.g. UInt64 or MutableInt8. Use theseclasses as you would int; arithmetic operations involving these classes will preservefixed width. For example:

Mutable instances can be modified in-place, preserving their type:

To set a mutable integer without losing its type, use slicing:

Arithmetic Operations

FixedInt instances support all arithmetic operators. For binary operators, bothoperands are converted to plain Python int and then operated on. With a fewexceptions, the result will be cast back to a FixedInt large enough to hold eitheroperand, provided one of the operands was a FixedInt. Note that the resultingFixedInt may not be large enough to hold the complete result, in which case theresult will be truncated.

The exceptions are as follows:

  • divmod returns a tuple of plain int s
  • true division returns a float
  • **, << and >> will return a FixedInt if the left operand was aFixedInt, and plain int otherwise.
Fixed

Mutable instances additionally support in-place operations, which will modify thevalue without altering its type.

Arithmetic operations between two integers of different sizes follow C integer promotionrules when determining the type of the final result. These rules boil down to thefollowing:

  • If the operands are both signed, or both unsigned, the wider of the two operand types is chosen.
  • Otherwise, if the unsigned operand is wider, the unsigned operand is chosen.
  • Otherwise, the signed operand is chosen.

Slicing

FixedInt instances support slicing. Slicing with a single integer produces a singleBoolean value representing the bit at that position. Slicing with a range produces aFixedInt containing the range of bits. Mutable instances additionally support sliceassignment. This makes e.g. manipulating a flag register straightforward, without needingto use bitwise operations.

All indexing operations treat the least-significant bit (LSB) as bit 0. Currently, onlycontiguous bit sections can be obtained; for more flexibility consider using a modulesuch as bitarray.

Getting a slice results in a FixedInt instance with exactly as many bits as the range.This can be used to perform wraparound arithmetic on a bit field.

Slices support two main syntaxes:

The latter syntax is more convenient when dealing with fixed-width fields. Both of theslice arguments may be omitted, in which case they will default to the LSB and MSB ofthe FixedInt respectively.

Byte Conversion

FixedInt instances can be converted to and from raw byte representations by using the.to_bytes instance method and the .from_bytes classmethod. The usage of thesemethods matches that of Python 3.4’s int.to_bytes and int.from_bytes methods.

Release historyRelease notifications | RSS feed

0.1.6

Python String Fixed Width

0.1.5

0.1.4

0.1.3

0.1.2

0.1.1

0.1.0

Download files

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

Files for fixedint, version 0.1.6
Filename, sizeFile typePython versionUpload dateHashes
Filename, size fixedint-0.1.6-py2-none-any.whl (12.7 kB) File type Wheel Python version py2 Upload dateHashes
Filename, size fixedint-0.1.6-py3-none-any.whl (12.7 kB) File type Wheel Python version py3 Upload dateHashes
Filename, size fixedint-0.1.6.tar.gz (12.8 kB) File type Source Python version None Upload dateHashes
Close

Hashes for fixedint-0.1.6-py2-none-any.whl

Hashes for fixedint-0.1.6-py2-none-any.whl
AlgorithmHash digest
SHA25641953193f08cbe984f584d8513c38fe5eea5fbd392257433b2210391c8a21ead
MD59735e323ce586585d0f3e4f87d1becac
BLAKE2-2565ffe5acd1a8ad94f118582f20aa0f4131b1536f38ded42ca5903db2fdb4b2af0
Close

Python Read Fixed Width

Hashes for fixedint-0.1.6-py3-none-any.whl

Python
Hashes for fixedint-0.1.6-py3-none-any.whl
AlgorithmHash digest
SHA256b8cf9f913735d2904deadda7a6daa9f57100599da1de57a7448ea1be75ae8c9c
MD51eb9f9fcbdf8f6bc0a1998a8be0346cf
BLAKE2-256c86d8f5307d26ce700a89e5a67d1e1ad15eff977211f9ed3ae90d7b0d67f4e66

Python Print Fixed Width Columns

Close

Python Fixed Width File

Hashes for fixedint-0.1.6.tar.gz

Python Fixed Width Parser

Hashes for fixedint-0.1.6.tar.gz
AlgorithmHash digest
SHA256703005d090499d41ce7ce2ee7eae8f7a5589a81acdc6b79f1728a56495f2c799
MD5758dfecd163714529bd9789eb4b9d506
BLAKE2-25632c6b1b9b3f69915d51909ef6ebe6352e286ec3d6f2077278af83ec6e3cc569c