[英]probleme old python script
hello everyone I have a problem with a python 2.X script that I would like to run in 3X here is the error message can you help me大家好,我想以 3X 运行的 python 2.X 脚本有问题,这里是错误消息,你能帮我吗
outfile.write(fixed.encode,"(utf-8)")
TypeError: write() takes exactly one argument (2 given)类型错误:write() 只需要一个参数(给出 2 个)
Edit:编辑:
#Write the converted program in this folder:
print ("\n--Converted to TI-Basic code:--")
print (fixed)
print ("")
print ("Making output files: "+outname+".tib, "+outname+".8xp ...")
outfile=open(outname+".tib","w")
outfile.write(fixed.encode("utf-8"))
outfile.close()
the code and the new errore message : line 68, in main outfile.write(fixed.encode("utf-8")) TypeError: write() argument must be str, not bytes代码和新错误消息:第 68 行,在 main outfile.write(fixed.encode("utf-8")) 类型错误:write() 参数必须是 str,而不是字节
That was probably caused by some rogue IDE autocomplete/Intellisense.这可能是由一些流氓 IDE 自动完成/智能感知引起的。
It should be:它应该是:
outfile.write(fixed.encode("utf-8"))
So, the quotes got switched around, not related to Python 2.x - 3.x conversion at all.因此,引号被切换了,与 Python 2.x - 3.x 转换完全无关。
The Error "TypeError: write() argument must be str, not bytes" is because you are trying to write in byte data but you didn't specified opened the file in byte mode.错误“TypeError:write() 参数必须是 str,而不是字节”是因为您尝试写入字节数据,但未指定以字节模式打开文件。 If you want to write string into a file use
with
statement如果要将字符串写入文件,请使用
with
语句
with open(outname+".tib","wb") as outfile:
outfile.write(fixed.encode("utf-8"))
If the fixed variable is str
type then you can avoid the encode('utf-8')
from the above code then the mode should be just w
如果固定变量是
str
类型,那么你可以避免上面代码中的encode('utf-8')
那么模式应该只是w
There appear to be missing parentheses in outfile.write(fixed.encode,"(utf-8)")
, the code looks jumbled. outfile.write(fixed.encode,"(utf-8)")
似乎缺少括号,代码看起来很混乱。
Your code could be cleaned up in a few areas, I made some quick tweaks for now.您的代码可以在几个方面进行清理,我现在进行了一些快速调整。
#Write the converted program in this folder:
print("\n--Converted to TI-Basic code:--")
print(fixed, end="\n\n")
print(f"Making output files: {out_name}.tib, {out_name}.8xp ...")
with open(f"{out_name}.tib", "w") as out_file:
out_file.write(fixed)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.