1

I have some Python scripts written with Python 2.7. I have to use that scripts with Python 3.x, but I have to change so many things like:

print "something"

to

print ("something")

because Python 3.x doesn't support print function without parenthesis. I don't want to do that manually because it will be too long and hard. I tried re module but failed. I am stuck, so any help will be appreciated.

8
  • 2to3 is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x docs.python.org/2/library/2to3.html Commented Jan 1, 2015 at 18:14
  • Im new Im sorry if this question is low quality... Commented Jan 1, 2015 at 18:25
  • its ok but its better that at least have a search before asking a question ! Commented Jan 1, 2015 at 18:28
  • Kasra I really search it I cant find and ask here.. Commented Jan 1, 2015 at 18:30
  • Then you are bad at searching: stackoverflow.com/q/1777767/3001761 Commented Jan 1, 2015 at 18:32

2 Answers 2

4

Try the automated 2to3 library

https://docs.python.org/2/library/2to3.html

$ 2to3 example.py

To make the changes directly

$ 2to3 -w example.py

STEPS

  1. Write your code in a file called example.py

    print "something"

  2. Save and close. Open a terminal and type

    2to3 -w example.py

  3. Open the file now. Tada ... the code is converted

    print(something)

Sign up to request clarification or add additional context in comments.

1 Comment

It one is going to run an automated conversion program, this is better, more general, and tested answer. The lib2to3 doc explains the use of the 2to3.py script. I have used it to convert a medium-sized package of multiple modules.
0

You can try something like this.

import re                 

with open("myprogram.py","r",encoding='utf8') as f:
    for x in f.readlines():
        y = x   # Main code x defining as y.
        if re.findall("print",x) == ['print']: # if 'print' is found;
            x = "print(" + x.split("print")[1].strip() + ")" # strip it quotation mark and paranthesis;
            y = y.replace(y,x)  #.. replace y with x.
        with open("different.py","a+") as ff:
            (y.strip()) # attention using \n for every sentence..

But it may break your code's design. So print them and copy-paste.

import re                 

with open("myprogram.py","r",encoding='utf8') as f:
    for x in f.readlines():
        y = x   # Main code x defining as y.
        if re.findall("print",x) == ['print']: # if 'print' is found;
            x = "print(" + x.split("print")[1].strip() + ")" # strip it quotation mark and paranthesis;
            y = y.replace(y,x)  #.. replace y with x.
        print(y.strip())

5 Comments

wow this is working. Im trying to understand x and strip part.
This is not a great idea. What if the word print appears in contexts other than as a statement (variable names, string literals, etc.)? It also doesn't handle e.g. the trailing comma bring replaced with the keyword argument end=''.
thankyou for answer It helped really thanks Im new in python sorry if this question is silly....
@whatwasthat why have you accepted this answer? It does not cover many of the common edge cases for print (or any of the other changes between 2.x and 3.x) and is not well implemented.
@jonrsharpe why have you accepted this answer? ... Now you know :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.