Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting to output file
#1
If I wanted to format decimal places in a print line, then I could do:

s = 37.3829
print('This is an example of formatting to 2 decimal places:  {:.2f}'.format(s))
What's the syntax for doing such formatting if printing to output_file.csv ?
Reply
#2
You're misunderstanding - there's no difference, because you're creating a string with the format you want and then passing that to print or the function that will write the line in the file.
Mark17 likes this post
Reply
#3
...and here's an example to illustrate:
s = 37.3829
output_string = 'This is an example of formatting to 2 decimal places:  {:.2f}'.format(s)

print (output_string)
with open ('tester.txt', 'w') as tester_file :
	tester_file.write (output_string)
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,300 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Formatting float number output barryjo 2 929 May-04-2023, 02:04 PM
Last Post: barryjo
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,115 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Is there a better way of formatting os.uname() output? CodeWolf 6 3,675 Aug-27-2021, 11:41 PM
Last Post: CodeWolf
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,633 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Logging to a file - Formatting Maxximiliann 0 1,448 Dec-18-2020, 02:06 AM
Last Post: Maxximiliann
  Output CSV file with filepath and file contents glittergirl 1 1,758 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Help! Formatting and Writing to a File bwdu 2 2,419 Apr-19-2020, 09:29 PM
Last Post: deanhystad
  csv file output rturus 7 3,263 Jan-30-2020, 02:09 PM
Last Post: buran
  Formatting file data written to file. melblanc 2 2,296 Jan-15-2020, 03:27 PM
Last Post: melblanc

Forum Jump:

User Panel Messages

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