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
  How to write variable in a python file then import it in another python file? tatahuft 4 906 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,056 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 904 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  To fetch and iterate data from CSV file using python vyom1109 3 1,008 Aug-05-2024, 10:05 AM
Last Post: Pedroski55
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,301 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 1,407 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Extracting the correct data from a CSV file S2G 6 1,773 Jun-03-2024, 04:50 PM
Last Post: snippsat
  encrypt data in json file help jacksfrustration 1 2,209 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 6,720 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Input network device connection info from data file edroche3rd 6 2,644 Oct-12-2023, 02:18 AM
Last Post: edroche3rd

Forum Jump:

User Panel Messages

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