allenrowand 0 Posted January 5 Share Posted January 5 Hello, I'm trying to strip the ending characters from the output of a python script and I can't get anything to work. The script is: import decimal query = float("{query}") output = str(round(decimal.Decimal((query*.029)+.3),2)) print(output) I've tried appending .strip() (and several other techniques) to the end of the output variable, but the newline is still there when I paste the result. I apologize if this is ridiculously simple, but I'm not used to working in python and am not much of a programmer. For what it's worth, the workflow asks for input and then computes how much Stripe is charging for payment processing. I put in the invoice amount and get the charge paid so that I can enter the expense in my books. As it stands when I paste the result into my financial software there's a trailing space and if I paste into a text editor I get a newline. Any help would be appreciated, thanks! Link to post
Mr Pennyworth 39 Posted January 5 Share Posted January 5 @allenrowand try this: import decimal import sys query = float("{query}") output = str(round(decimal.Decimal((query*.029)+.3),2)) sys.stdout.write(output) sys.stdout.flush() The newline is coming from the `print` function Link to post
deanishe 1,368 Posted January 5 Share Posted January 5 5 hours ago, Mr Pennyworth said: sys.stdout.write(output) sys.stdout.flush() Don’t need the flush() at the end of a script. Another possibility is print(output, end='') as long as you’re using Python 3 or from __future__ import print_function in Py2. Mr Pennyworth 1 Link to post
allenrowand 0 Posted January 5 Author Share Posted January 5 7 hours ago, deanishe said: Another possibility is print(output, end='') as long as you’re using Python 3 or from __future__ import print_function in Py2. I tried print(output, end='') previously and the result was blank. And now I know my system is running python 2.7… Link to post
deanishe 1,368 Posted January 5 Share Posted January 5 (edited) 2 hours ago, allenrowand said: I tried print(output, end='') previously and the result was blank. And now I know my system is running python 2.7… That is an error in 2.7 if you don’t have from __future__ import print_function at the top of the script. The error would certainly have been visible in Alfred's debugger. Edited January 5 by deanishe Link to post
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now