Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Right way of PRINTING values in python3?

Tue Oct 14, 2014 6:59 pm

Hello all,
i'm getting crazy trying to simply print something in python3. So i hope in any of you explayning me what's the strange idea there's behind this :)

I come from php, so for me it was "echo $something" and php was casting the var to what it was needed to be printed.

Then python 2, i had to "cast" everything with a (simple) str()

For example, to print my wind sensor reading i can do something like this

Code: Select all

			print("TX23 sensor reading")
			print("Datetime: " + datetime.datetime.fromtimestamp(output['timestamp']).strftime('%d/%m/%Y - %H:%M:%S'))
			print("Speed: " + str(data['speed']) + " m/s")
			print("Direction: " + str(data['direction']))
			print("Degrees: " + str(data['degrees']) + "°")
But at the end came python3 :)

Can you see that code? it works if i execute the script from cli.
Works good.
But if i execute it from a web page the magic:
- Error: 'ascii' codec can't encode character '\xb0' in position 18: ordinal not in range(128)

wtf?? it is going well from cli!

So since this point i really lost myself.. tried encode("utf-8"), tried format, but i simply did not understand the ratio so i'm moving blindly :)

Please guide me :)

PS: the script itself should be utf-8 (i added the #coding=utf-8 at the start of it), but it's clear i'm missing something bigger :)

Thanks

PS: i'd like more something working in python 2 AND 3..

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: Right way of PRINTING values in python3?

Wed Oct 15, 2014 12:22 am

Try this, do you still have the error :

Code: Select all

print(
"""
TX23 sensor reading
Datetime: {dt:s} 
Speed: {sp:s} m/s
Direction: {di:s}
Degrees: {de:s}"""
.format( dt=datetime.datetime.fromtimestamp(output['timestamp']).strftime('%d/%m/%Y - %H:%M:%S')),
         sp=data['speed'],
         di=data['direction'],
         de=data['degrees']))
I have spaced the line out to make it clearer.
The triple quotes allow you to specify a multi-line string.
The places to add your data are bounded by {} .
Each data item is defined by a label in the format statement.
I don't know what your data types are, the "s" is just that I have just assumed strings in the example.
So in the print string a data item is specified for example as {sp:s}
However, your data might be something else, it would be
{sp:d} for an integer
{sp:6.2f} for a floating point number, in this case 6 characters including the point, with two digits after the point.

Substituting data values into strings is more sophisticated than this example. This tutorial might help :

http://www.python-course.eu/python3_for ... output.php

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Right way of PRINTING values in python3?

Wed Oct 15, 2014 8:19 am

thanks mate, it's working :)

1) i hate "space saving" of python.. lol..
2) i haven't found a way to manage "float as integers" values.. i mean, in the example my degrees direction can be 292.5° OR 180°. In the second case, i'll always have 180.0 since i have to declare the var as float to have the .5 (am i right?)
3) is it ok to declare float var in this way

Code: Select all

"Speed: {sp:0.1f} m/s"
if i want to avoid python to reserve spaces for values?

Thanks again :)

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Right way of PRINTING values in python3?

Wed Oct 15, 2014 8:49 am

lol, the new problem is that i get the "encode" error if i simply try to print the degree char

Code: Select all

print("°°°°°°°")
but at this poing i begin to think that it's a problem of php exec.. i tried to set local env var to something UTF-8, but with no luck..

Edit: solved in this way: python is using wrong encoding when called by php, so i had to "remember" it that the default encoding is UTF-8.
That's an env var of python i could set with php before executing scripts

Code: Select all

putenv('PYTHONIOENCODING=UTF-8');
Thanks again :)

User avatar
paddyg
Posts: 2602
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Right way of PRINTING values in python3?

Wed Oct 15, 2014 10:59 am

wrt your questions 2) and 3) you might be able to get something like what (I think) you are after by using

Code: Select all

'Degrees {:.4g}°'.format(data['degrees'])
This will render 292.498625 as 292.5° but 180.0 as 180° but numbers over 9999 will be in scientific notation (presumably not an issue for compass directions) and numbers less than 100 will render to two decimal places if they're in the float i.e. 76.234 as 76.23° but 76.0 as 76°

PS you don't need to put the field names in the format curly brackets, it's useful if you want to put the same data into the string in different locations but often more readable without.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Right way of PRINTING values in python3?

Wed Oct 15, 2014 11:09 am

it works!

I admit i hadn't understood this behaviour of "g" from this description:
e Floating point exponential format (lowercase).
f Floating point decimal format.
g Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise.
Thanks :)

Return to “Python”