Bottom Page

Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
template string format specifiers
#1
>>> f'{id:3s} : {location:19s} : {max_temp:3d} / {min_temp:3d} / {precipitation:5:2f}'.format(
id=id, location=location, max_temp=max_temp,
min_temp=min_temp, precipitation=precipitation
)
Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
f'{id:3s} : {location:19s} : {max_temp:3d} / {min_temp:3d} / {precipitation:5:2f}'.format(
ValueError: Invalid format specifier

--Why am I getting this error? Please help. Thanks. All variables are assigned and match
Quote
#2
Its 5.2f not 5:2f
Quote
#3
Than you very much!
metulburr likes this post
Quote
#4
You are also mixing f-string and format().
It can be done a easier with f-string,then there no need for fomat() at all.
id = 1000
location = 'fooooooooo'
max_temp = 40000.458545
min_temp = 1000.7895642
precipitation = 7575.45454

print(f'{id:d} : {location:.3} : {max_temp:.2f} / {min_temp:.2f} / {precipitation:.1f}')
Output:
1000 : foo : 40000.46 / 1000.79 / 7575.5
Also id is a name used bye Python,so use something else eg temp_id = 1000.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  code help to convert log file to csv format bmunagala 3 107 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Merge 2 Excel in a specific format Apython 0 58 Jan-16-2019, 07:26 AM
Last Post: Apython
  About the output format. shang2019 4 165 Jan-10-2019, 07:38 PM
Last Post: buran
  Format text and variables - more lines dan789 3 140 Jan-08-2019, 03:07 PM
Last Post: buran
  How to check UUID format and verion swamyjrs 1 113 Dec-19-2018, 04:20 PM
Last Post: Larz60+
  OMDB API KEY format Linuxdesire 4 115 Dec-11-2018, 09:29 PM
Last Post: Larz60+
  Python for Reports in HTML or PDF Format ImPyBoy17 1 150 Dec-10-2018, 09:35 PM
Last Post: j.crater
  python 3 memoryview : unsupported format <d evason 0 131 Nov-21-2018, 04:27 PM
Last Post: evason
  Stream Data to Text or CSV format ekq378 0 154 Nov-16-2018, 06:32 PM
Last Post: ekq378
  Counting the number of files related to particular format ambush 3 238 Nov-05-2018, 08:58 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)