我是Python新手,我意识到我不能像JavaScript那样连接replace
:
#!/usr/bin/env python
import os
import re
import string
for filename in os.listdir("."):
if filename.endswith(".png"):
new_filename = string.replace(filename, "2x", "@3x").replace(filename, "-lanczos3", "")
os.rename(filename, new_filename)
我得到这个错误:
File "hello.py", line 8, in <module>
new_filename = string.replace(filename, "2x", "@3x").replace(filename, "-lanczos3", "")
TypeError: an integer is required
Python的方法是什么?你知道吗
您正在对^{} (返回} (方法)。你知道吗
str
对象的函数)的返回值调用^{只需使用以下方法:
错误源于
str.replace()
的第三个参数,该参数必须是限制替换次数的整数。你基本上做到了:其中
""
参数不是整数。你知道吗您可以对
string.replace()
执行相同的操作,但是必须将一个调用的结果作为另一个调用的第一个参数传入:但是,您不想这样做,因为
string
函数已被弃用;请参见documentation:该部分中的任何函数都具有直接在
str
类型本身上可用的等效方法。你知道吗我对python也很陌生,但我的猜测是要改变这一行:
分为:
相关问题 更多 >
编程相关推荐