Python Forum
python2 string formatting - old and new - different for unicode
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python2 string formatting - old and new - different for unicode
#1
replying to another thread, I noticed something strange. following script, running python 2.7


import forecastio

def main():
    api_key = "API KEY"
    lat = -31.967819
    lng = 115.87718
    forecast = forecastio.load_forecast(api_key, lat, lng) 
    by_day = forecast.daily()

    print "===========Daily Data========="
    print "Daily Summary: %s" %(by_day.summary)
    
    print "===========Daily Data========="
    print "Daily Summary: {}".format(by_day.summary)

if __name__ == "__main__":
    main()
and the result

Output:
===========Daily Data========= Daily Summary: Light rain on Friday through Monday, with temperatures falling to 15°C on Saturday. ===========Daily Data========= Traceback (most recent call last):  File "dark.py", line 53, in <module>    main()  File "dark.py", line 46, in main    print "Daily Summary: {}".format(by_day.summary) UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 68: ordinal not in range(128)
so old-style string formatting has no problem with unicode char, while new one - using format, raise error. The behavior still the same if explicitly specify {:s}. I find it odd that there is difference. Do I miss something?
Reply


Messages In This Thread
python2 string formatting - old and new - different for unicode - by buran - May-16-2017, 09:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,303 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 853 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 2,009 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 2,075 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 9,431 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Question on HTML formatting with set string in message Cknutson575 3 3,509 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,788 May-15-2020, 01:37 PM
Last Post: snippsat
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,665 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  Unicode string index problem luoheng 6 3,053 Nov-23-2019, 03:04 PM
Last Post: luoheng
  Trying to run a python2 script dagamer1991 3 2,558 Aug-12-2019, 12:33 PM
Last Post: buran

Forum Jump:

User Panel Messages

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