- C:\Users\Administrator>python3 F:\case\operate.py
- File "F:\case\operate.py", line 46
- print 'str=', str
- ^
- SyntaxError: Missing parentheses in call to 'print'
复制代码
原因:
python2.X版本与python3.X版本输出方式不同造成的
解决方案:
Print函数后面加上括号()
1.1.1 Python2代码与Python3代码不兼容
其实将python代码用python3执行,很多问题都是由于python2版本和python版本代码不兼容导致的。
下面以实际的例子,介绍一种python2代码转换成python3代码的方法:
假设:python 2.X到python 3.X转换文件2to3.py的路径为C:\Python36\Tools\scripts
待转换的文件operate.py的路径为D:\python2to3
CMD下执行如下命令,即可将该python2文件转换为python3文件
python3 C:\Python36\Tools\scripts\2to3.py -wD:\python2to3\operate.py
出现如下打印说明转换过程成功
- C:\Users\Administrator>python3 C:\Python36\Tools\scripts\2to3.py -w D:\python2to3\operate.py
- RefactoringTool: Skipping optional fixer: buffer
- RefactoringTool: Skipping optional fixer: idioms
- RefactoringTool: Skipping optional fixer: set_literal
- RefactoringTool: Skipping optional fixer: ws_comma
- RefactoringTool: Refactored D:\python2to3\operate.py
- --- D:\python2to3\operate.py (original)
- +++ D:\python2to3\operate.py (refactored)
- [url=home.php?mod=space&uid=40658]@@[/url] -1,7 +1,8 @@
- # coding:utf-8
- import os,re,xlrd
- import sys
- -reload(sys)
- +import imp
- +imp.reload(sys)
- sys.setdefaultencoding("utf-8")
- from easy_excel import easy_excel
- @@ -43,7 +44,7 @@
- def actions_add_div(str):
- if str:
- new_action_str=""
- - print 'str=', str
- + print('str=', str)
- for str_index in str.split('\n'):
- newline=str_index + "" + "</div>" + "<div>" + "" + "</p>" + "<p>"
- new_action_str+=newline
- @@ -122,16 +123,16 @@
- if __name__ == "__main__":
- - print os.path.abspath('.')
- + print(os.path.abspath('.'))
- excellist=[]
- #列出当前目录下的所有.xml文件
- for fullname in iterbrowse(os.path.abspath('.')):
- - print fullname
- + print(fullname)
- obj1=re.compile(r'([\W\w]*)(\.xlsx)
- [/color][/align]
- [/backcolor]
- [backcolor=rgb(247, 247, 247)]
- [/backcolor]
- )
- for m in obj1.finditer(fullname):
- - print m.group()
- + print(m.group())
- excellist.append(m.group())
- - print excellist
- + print(excellist)
- for fileName in excellist:
- fileName=fileName.split('\\')[-1]
- file_data = xlrd.open_workbook(fileName)
- @@ -139,12 +140,12 @@
- sheetList=[]
- for index in range(sheetnum):
- sheet_name=file_data.sheets()[index].name
- - print sheet_name
- + print(sheet_name)
- sheetList.append(sheet_name)
- - print sheetList
- + print(sheetList)
- for sheetName in sheetList:
- test = operate(fileName, sheetName)
- test.xlsx_to_dic(sheetName)
- test.dic_to_xml(fileName, sheetName)
- - print "Convert success!"
- + print("Convert success!")
- os.system('pause')
- RefactoringTool: Files that were modified:
- RefactoringTool: D:\python2to3\operate.py
复制代码