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
  Learn to use a JSON file to fill out Template Automatically Srotkowitz 1 77 Mar-06-2019, 08:15 PM
Last Post: Larz60+
  String format checking? MuntyScruntfundle 1 68 Mar-06-2019, 12:01 PM
Last Post: buran
  Change Time Format in Python bluethundr 2 66 Mar-04-2019, 09:13 PM
Last Post: bluethundr
  How to format text to modify itself for different console sizes. FWendeburg 0 60 Feb-18-2019, 06:43 PM
Last Post: FWendeburg
  int format for 001 - 999 jmair 4 154 Feb-10-2019, 05:04 PM
Last Post: jmair
  Change format of datetime Jonas85 6 228 Feb-05-2019, 03:47 PM
Last Post: Jonas85
  Read data of Wireless Modem in Byte Format barry76 2 138 Jan-31-2019, 11:26 AM
Last Post: barry76
  code help to convert log file to csv format bmunagala 3 225 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Merge 2 Excel in a specific format Apython 0 119 Jan-16-2019, 07:26 AM
Last Post: Apython
  About the output format. shang2019 4 265 Jan-10-2019, 07:38 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)