Python Forum
Formatting file data written to file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting file data written to file.
#1
I have four columns of data in a file created and written to
by a Python script.

The first column is a line entry number beginning with, '1'
with increment by '1' for each additional line written to the
file. The data entries are comma separated.

I would like to right justify the first column but cannot seem
to identify a method which works. The reason for right hand
justification is to make the displayed file output easier to
read. Presently the file output looks like this:

1,    15:45:08.335,    120456321, 80446723
7,    15:47:14.456,    120309625, 80447991
10,    15:50:22.920,    120225771, 80505351
102,    15:59:38.333,    119889999, 80504999
The above formatting makes it difficult to read. What I want
to do is to write the data to the file such that entering the
following: cat filename.txt which contains the above displayed
data will display the data in the format shown below:

  1,    15:45:08.335,    120456321, 80446723
  7,    15:47:14.456,    120309625, 80447991
 10,    15:50:22.920,    120225771, 80505351
102,    15:59:38.333,    119889999, 80504999
I an using the following script to write the results stored in the
four variables to "output_filename":

  with open(output_filename, 'a') as g:
              g.write(str(z)+",   "+time+",    "+velocity1+" , "+velocity2 "\r\n")  
Is there a way to right hand justify the variable entry 'z' through formatting
like is done in the print statement with txt.format.

TIA

Mel
Reply
#2
You could use string formatting:
g.write(f'{z:>5}, {time}, {velocity1}, {velocity2}\r\n')
With format method:
g.write('{:>5}, {}, {}, {}\r\n'.format(z, time, velocity1, velocity2))
Here is an overview: pyformat.info
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Dead Eye, many thanks and also double thanks for the referral to the pyformat.info
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 188 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,080 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Input network device connection info from data file edroche3rd 6 994 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  Need to replace a string with a file (HTML file) tester_V 1 754 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 918 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Convert File to Data URL michaelnicol 3 1,145 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,248 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,802 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Correctly read a malformed CSV file data klllmmm 2 1,911 Jan-25-2023, 04:12 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020