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 all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Formatting a data file

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


    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: 38,805 Mod ✭✭✭✭Seth Brundle


    Excel?


  • Registered Users Posts: 8,052 ✭✭✭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 Posts: 7,697 ✭✭✭StupidLikeAFox




  • Registered Users Posts: 8,052 ✭✭✭funkey_monkey


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


  • Registered Users Posts: 1,701 ✭✭✭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