what to type on a text editor find-and-replace to port python 2 prints to python3 - python-3.x

I am trying to make this open source project to run in python 3.
So far the only fix is to replace print whatever to print(whatever)
Is there any way to tell the find and replace tool of pycharm to do this?

Don't use a text editor for this. Use 2to3 instead. It comes with Python.

Related

How to open program with key input

I want to open the calculator program when I type "Open calculator " . I researched a lot , but didn't get the answer I wanted.Can anyone please answer my question.
There are 2 ways to do this. One way would be access the cmd using Python which can be done by using os module. When you try to open the calculator from your command prompt, you probably type calc. Instead of manually doing this, you can have your Python code do it for you, this is how:
import os
os.system('calc')
The second way is very similar to the first one, except that this method opens another command prompt window so the window in which you're running the python code is not disturbed.
import subprocess
subprocess.Popen("calc",stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True)

How do I use output from SPARC solver as input to a python file?

My question: I need to save the output from a SPARC solver (which is currently appearing as text in the terminal) as a variable in my Python code. How can I do this?
Quick note: SPARC is a solver used for ASP (answer-set-programming) files - just mentioning this so that people don't get confused and think I am referring to asp.net.
I am running a Python file and an ASP file (in query mode) simultaneously, in the same terminal, using the command python pythonfile.py | java -jar sparc.jar aspfile.sp. Output from the python file in the form of sys.stdout.write() is being redirected as the input to the SPARC solver; i.e. the text I output becomes the query that is solved using my ASP code. This is working, and generating the output I want, but I can't figure out how to use that output in my Python code.
This is a follow-up to another question I have asked, found here. In that question I was trying to find out how to run an ASP file from my C++ code. I went with option 2 in the answer I was given, and am using redirected stdout with the two files running as separate processes. Please note one major change since the original question: I am now using Python instead of C++.
Further details if required: My Python version is 2.7 and my operating system is Ubuntu 14.04. I don't think it's relevant but, in case it is, you should know that my Python code is also being used to control a Gazebo Turtlebot simulation, and I am using ROS Indigo to run that simulation. I won't post my code unless someone requests it, as I just want an idea of what method I could use (I can't find anything that works on the internet), rather than needing my code debugged / assistance writing it. However I will post below an example of what is output to the terminal when my code runs, as this is the information I am trying to 'capture'.
SPARC V2.52
program translated
?- yes
?- no
It is the answers 'yes' and 'no' that I want to save as variables in my Python file.
SOLUTION:
For anyone wanting to do the same thing, I followed the answer provided by CaptainTrunky.
First I run the command python pythonfile.py | java -jar sparc.jar aspfile.sp > sparc.out, saving the SPARC output to the text file sparc.out.
Then I run python outputParser.py to run a script that prints the contents of the text file, allowing me to check that I'm manipulating the data correctly. The script is very simple:
lines = [line.strip('?- ') for line in open('sparc.out')]
lines = [line.strip('\n') for line in lines]
print lines
You can use python to read from sys.stdin with a script similar to the following (filter.py):
import sys
for line in sys.stdin.readlines():
if line.startswith('?- '):
print line.strip()
Then invoke your pipeline like this:
python pythonfile.py | java -jar sparc.jar aspfile.sp | python filter.py
I would suggest you do dump SPARK output to text file and then to parse it with you tool.
Write a shell script that does it for you:
python pythonfile.py | java -jar sparc.jar aspfile.sp > spark.out
python parse_out.py spark.out

python3 input using sublime text 3

I am trying to use the input feature on python for my program. I installed SublimeREPL but I still cant seem to figure out how to provide an input for my program. (coming from a complete beginner)
Using input from within an editor can be tricky. A simple solution would be to run your program from the command line. Open a terminal and change into the directory where your script user_inputs_intro.py is located and type:
python user_inputs_intro.py
Now, you will see the Tell me something: and should be able to type something that will be echoed back after you press enter.

how to detect/ remove 'UTF-8' code in python (window 10 console )

saw some answers on this but not really useful
I do some coding with python using blender 2.6
and on occasion I copy text from internet page or from Libre office text document
so there might be some control characters UTF-8 copied to the text editor for python in blender
problem is I cannot detect where UTF-8 characters are located in blender text editor or even using outside text editor like notepad 2 or notepad ++
so my question is there a simple way to detect and remove these UNTF-8 characters ?
I mean on window 10 in blender using some python commands or using external text editor!
I need something quick or a simple trick here if possible!
sorry it is "utf-8"
in blender at least the error is given as "utf-8"
and very annoying
so when I run a script in bl with some unknown characters it gives an error
the problem is how can I find where it is and remove it !
The error in blender is given but does not always shows where it is located in the python script!
even if i had to use a text editor that can locate and then help to remove these characters somehow if possible at all

Novice with Python

I have just downloaded Python 3.3.2 to use on windows7 and run the msi file to install. After installation I have tried using the prog only to find that every time I run my initial print 'hello world' it keeps reporting a syntax error.
I have tried both single and double quotes but each time reports a syntax. It will add say 8 + 9 and return the answer but no joy with using a print statement.
I have tried both the shell and a new window but without success.
Any advice please much appreciated.
If you are using Python 3.x, you have to do print('hello world').
In Python 2.x, print was a statement, like if, for, etc. Now it is a function, thus the ().
You're probably using instructions for a python-2 program, where print was a statement, rather than a function. In python >= 3, you have to do print(something), rather than just print something.

Resources