Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Formatting a data file

  • 18-03-2021 12:00pm
    #1
    Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭


    Hi,

    I've got a data file which I need re-format in order that it can be input to an old piece of analysis software.
    Basically I need to adjust gaps between columns of data and also right justify some data.

    What is the best way to go about doing this?


    Thanks.


Comments

  • Moderators, Politics Moderators Posts: 41,240 Mod ✭✭✭✭Seth Brundle


    Excel?


  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    I'm doing it in Python - found a script which does something similar.

    How do I ensure a number such as 12.258 is presented as 000012.258 in Python?


  • Registered Users, Registered Users 2 Posts: 7,745 ✭✭✭StupidLikeAFox




  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    Thanks - found zfill when searching and it seems to have done what I need.


  • Registered Users, Registered Users 2 Posts: 1,704 ✭✭✭JoyPad


    I'm doing it in Python - found a script which does something similar.

    How do I ensure a number such as 12.258 is presented as 000012.258 in Python?

    >>> print("%010.3f" % 12.345)
    000012.345
    

    "f" is for floating point values, and it has two numbers, separated by a dot:
    %M.Nf means M total digits (including the decimal point), of which N are decimal places. Prefixing M with a 0 means showing leading zeroes, instead of spaces.

    So, in the example above, we have 10 total digits, of which 6 will be allocated for the integer part, and 3 for the decimal places, leaving one for the dot.

    If you have a number of fields that you want to output on the same line, you can use a larger format sequence:
    >>> name = "JoyPad"
    >>> salary = 180000
    >>> daily = 695.956
    >>> print("%20s%010d%010.3f" % (name, salary, daily))
                  JoyPad0000180000000695.956
    

    So, the name is printed using maximum 20 characters, padded with spaces, using 's' specification for String.
    The salary is printed as a integer number, using 'd', on 10 characters, padded with zeroes.
    The daily rate is printed with the format discussed above.

    This is a common type of file format used in accounting and banking, called fixed width format.


  • Advertisement
Advertisement