字符串查找和替换 [英] String find and replace
问题描述
import os,string
print"
setpath = raw_input("输入路径:")
def find_replace(setpath):
for root,dirs, os.walk中的文件(setpath):
fname = files
文件中fname的
:
find = string.find(file(os) .path.join(root,fname),
''rb'')。read(),''THIS'')
打印发现
如果找到> = 1:
replace = string.replace(str,''THIS'',''THAT'')
find_replace(setpath)
print"
为什么这不起作用?我收到此错误:
Traceback(最近一次调用最后一次):
文件" html_find_replace.py",第12行,在?
find_replace(setpath)
文件" html_find_replace.py",第11行,在find_replace中
replace = string.replace(str,''THIS'' ,''那个')
文件" /usr/local/lib/python2.3/string.py" ;,第370行,替换
返回s。替换(旧的,新的,maxsplit)
TypeError:预期的字符缓冲区对象
" hokieghal99" <豪******** @ hotmail.com>在消息中写道
news:bi ********** @ solaris.cc.vt.edu ...import os,string
打印 "
setpath = raw_input("输入路径:")
def find_replace(setpath):
用于root,dirs,os.walk中的文件(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),
''rb'')。read() ,''这个'')打印发现
如果找到> = 1:
replace = string.replace(str,''THIS'',''THAT'')
find_replace(setpath)
print"
为什么这不起作用?我收到此错误:
Traceback(最近一次调用最后一次):
文件" html_find_replace.py",第12行,在?
find_replace(setpath)
文件html_find_replace.py,第11行,在find_replace中
replace = string.replace(str,''THIS'',''THAT'')
文件" / usr / local / lib /python2.3/string.py" ;,第370行,替换
返回s.replace(old,new,maxsplit)
TypeError:期望一个字符缓冲对象
什么'是'str"在第11行?
John Roth
hokieghal99写道:import os ,string
print" "
setpath = raw_input("输入路径:")
def find_replace(setpath):
用于root,dirs,os.walk中的文件(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),''rb'')。read(),
''这个'')打印发现
如果找到> = 1:
replace = string.replace(str,''THIS'',''THAT'')
^^^
在你的应用程序中,你认为''str'是什么?
你还没有定义它,但它仍然存在。它是字符串类型,*不是*
一个特定的字符串(因为你没有定义它)。这就是为什么你得到下面的
错误:
find_replace(setpath)
print"
为什么这不起作用?我收到此错误:
Traceback(最近一次调用最后一次):
文件" html_find_replace.py",第12行,在?
find_replace(setpath)
文件html_find_replace.py,第11行,在find_replace中
replace = string.replace(str,''THIS'',''THAT'')
文件" / usr / local / lib /python2.3/string.py" ;,第370行,替换
return s.replace(old,new,maxsplit)
TypeError:期望一个字符缓冲区对象
- Gerhard
感谢您的解释,我可以这样做:
import os,string
setpath = raw_input("输入路径:")
for root,dirs,os.walk中的文件(setpath) :
fname =文件
x =''这个''
$ b $ = ='''那个'\\ n \\ n>
for fname在文件中:
myfile = file(os.path.join(root,fname),''r'')
mystr = myfile.read()
myfile.close()
search = string.find(mystr,x)
如果搜索> = 1:
string.replace(mystr,x,y)
print" Replacing",x," with",y," in",fname
如果我真的可以对文件进行更改!它适用于
理论,但不是在实践中;)任何人都建议如何将
更改实际写入文件?我是新手,所以要善待。
谢谢大家!!!
Geoff Gerrietts写道:引用hokieghal99(ho********@hotmail.com):
import os,string
print" "
setpath = raw_input("输入路径:")
def find_replace(setpath):
用于root,dirs,os.walk中的文件(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),''rb'')。read(),''THIS '')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
这个字符串从不绑定名称
(即,你永远不会指定str = file(...)。read())
print find
如果找到> = 1:
replace = string.replace(str,''THIS'',''THAT'')
^^^
这个名字目前已经绑定了内置函数str()
find_replace( setpath)
打印
您可能会考虑下面的片段。这是几行更长,但更安全(你的.close()恰好发生在你想要的时候)
并且可能更具可读性。
对于root,dirs, os.walk中的文件(setpath):
fname = files
文件中的fname:
myfile = file(os.path.join(root,fname),'rb'')
mystr = myfile.read()
myfile.close()
find = string.find(mystr,''THIS'')
打印查找
如果找到> = 1:
replace = string.replace(mystr,''这个'',''那个')
运气,
- G。
import os, string
print " "
setpath = raw_input("Enter the path: ")
def find_replace(setpath):
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),
''rb'').read(), ''THIS'')
print find
if find >=1:
replace = string.replace(str, ''THIS'', ''THAT'')
find_replace(setpath)
print " "
Why doesn''t this work? I get this error:
Traceback (most recent call last):
File "html_find_replace.py", line 12, in ?
find_replace(setpath)
File "html_find_replace.py", line 11, in find_replace
replace = string.replace(str, ''THIS'', ''THAT'')
File "/usr/local/lib/python2.3/string.py", line 370, in replace
return s.replace(old, new, maxsplit)
TypeError: expected a character buffer object
"hokieghal99" <ho********@hotmail.com> wrote in message
news:bi**********@solaris.cc.vt.edu...import os, string
print " "
setpath = raw_input("Enter the path: ")
def find_replace(setpath):
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname),
''rb'').read(), ''THIS'')
print find
if find >=1:
replace = string.replace(str, ''THIS'', ''THAT'')
find_replace(setpath)
print " "
Why doesn''t this work? I get this error:
Traceback (most recent call last):
File "html_find_replace.py", line 12, in ?
find_replace(setpath)
File "html_find_replace.py", line 11, in find_replace
replace = string.replace(str, ''THIS'', ''THAT'')
File "/usr/local/lib/python2.3/string.py", line 370, in replace
return s.replace(old, new, maxsplit)
TypeError: expected a character buffer object
what''s "str" in line 11?
John Roth
hokieghal99 wrote:import os, string
print " "
setpath = raw_input("Enter the path: ")
def find_replace(setpath):
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname), ''rb'').read(),
''THIS'')
print find
if find >=1:
replace = string.replace(str, ''THIS'', ''THAT'') ^^^
In your app, what do you think ''str'' is?
You haven''t defined it, but it still exists. It''s the string type, *not*
a particular string (cos you haven''t defined it). That''s why you get the
error below:
find_replace(setpath)
print " "
Why doesn''t this work? I get this error:
Traceback (most recent call last):
File "html_find_replace.py", line 12, in ?
find_replace(setpath)
File "html_find_replace.py", line 11, in find_replace
replace = string.replace(str, ''THIS'', ''THAT'')
File "/usr/local/lib/python2.3/string.py", line 370, in replace
return s.replace(old, new, maxsplit)
TypeError: expected a character buffer object
-- Gerhard
Thanks for the explanation, I can make it work this way:
import os, string
setpath = raw_input("Enter the path: ")
for root, dirs, files in os.walk(setpath):
fname = files
x = ''THIS''
y = ''THAT''
for fname in files:
myfile = file(os.path.join(root,fname), ''r'')
mystr = myfile.read()
myfile.close()
search = string.find(mystr, x)
if search >=1:
string.replace(mystr, x, y)
print "Replacing", x, "with", y, "in", fname
If only I could actually make the change to the files! It works in
theory, but not in practice ;) Anyone recommend how to actual write the
change to the file? I''m new to this, so be kind.
Thanks Everyone!!!
Geoff Gerrietts wrote:Quoting hokieghal99 (ho********@hotmail.com):import os, string
print " "
setpath = raw_input("Enter the path: ")
def find_replace(setpath):
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
find = string.find(file(os.path.join(root,fname), ''rb'').read(), ''THIS'')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this string is never bound to a name
(ie, you never assign str=file(...).read())print find
if find >=1:
replace = string.replace(str, ''THIS'', ''THAT'')
^^^
this name is currently bound to
the builtin function str()find_replace(setpath)
print " "
You might consider the fragment below, instead. It''s a couple lines
longer, but safer (your .close() happens exactly when you want it to)
and probably more readable.
for root, dirs, files in os.walk(setpath):
fname = files
for fname in files:
myfile = file(os.path.join(root,fname), ''rb'')
mystr = myfile.read()
myfile.close()
find = string.find(mystr, ''THIS'')
print find
if find >=1:
replace = string.replace(mystr, ''THIS'', ''THAT'')
Luck,
--G.
这篇关于字符串查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!