文件问题
file_name = input('请输入文件名:')import cchardet
with open(file_name, 'rb') as f:
encoding = cchardet.detect(f.read(1024))['encoding']
def replace_file(file_name,new_line,b):
with open(file_name,'r+', encoding=encoding) as f:
g = f.readlines()
f.seek(0,0)
for i in g:
if new_line in i:
i_new = i.replace(new_line,b)
f.write(i_new)
def count_line(file_name,rep_word):
import re
with open(file_name, encoding=encoding) as f:
message = ''
for line in f:
message += line.rstrip()
c = re.findall(rep_word,message) #这里是指message中所有和new_line相同的一个数组
count = len(c)
return count
new_line = input('请输入需要替换的文字:')
b = input('请输入新的文字:')
c = count_line(file_name,new_line)
print('文件%s中共有%s个%s'%(file_name,c,new_line))
print('您确定要把所有的%s都替换成%s:'%(new_line,b))
temp = input('(YES/NO):')
if temp == 'YES' or 'yes':
replace_file(file_name,new_line,b)
问题:为啥我这样取代文字有问题,我不知道怎么修改。 file_name = input('请输入文件名:')
import cchardet
with open(file_name, 'rb') as f:
encoding = cchardet.detect(f.read(1024))['encoding']
def replace_file(file_name,new_line,b):
with open(file_name,'r+', encoding=encoding) as f:
g = f.readlines()
f.seek(0,0)
for i in g:
if new_line in i:
i = i.replace(new_line,b) # 这里应该用 i,因为不能只把替换过的写回去,还要把原来的写回去
f.write(i) # 用 i
def count_line(file_name,rep_word):
import re
with open(file_name, encoding=encoding) as f:
message = ''
for line in f:
message += line.rstrip()
c = re.findall(rep_word,message) #这里是指message中所有和new_line相同的一个数组
count = len(c)
return count
new_line = input('请输入需要替换的文字:')
b = input('请输入新的文字:')
c = count_line(file_name,new_line)
print('文件%s中共有%s个%s'%(file_name,c,new_line))
print('您确定要把所有的%s都替换成%s:'%(new_line,b))
temp = input('(YES/NO):')
if temp == 'YES' or 'yes':
replace_file(file_name,new_line,b) file_name = input('请输入文件名(E:/1.txt):')
import cchardet
import re
with open(file_name, 'rb') as f:
encoding = cchardet.detect(f.read(1024))['encoding']
def count_line(file_name,new_line):
with open(file_name) as b:
g = b.readlines()
b.seek(0,0)
message = ''
for line in g:
message += line.rstrip()
c = re.findall(new_line,message)#这里是指message中所有和new_line相同的一个数组
count = len(c)
return count
def file_replace(file_name, rep_word, new_word):
with open(file_name, 'r+', encoding=encoding, errors='ignore') as f:
content = []
count = count_line(file_name,rep_word)
for eachline in f:
if rep_word in eachline:
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n文件 %s 中共有%s个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:'
% (file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
with open(file_name, 'w') as f:
f.writelines(content)
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)
能看下这个代码哪里错了吗? file_name = input('请输入文件名(E:/1.txt):')
import cchardet
import re
with open(file_name, 'rb') as f:
encoding = cchardet.detect(f.read(1024))['encoding']
def count_line(file_name,new_line):
with open(file_name) as b:
g = b.readlines()
b.seek(0,0)
message = ''
for line in g:
message += line.rstrip()
c = re.findall(new_line,message)#这里是指message中所有和new_line相同的一个数组
count = len(c)
return count
def file_replace(file_name, rep_word, new_word):
with open(file_name, 'r+', encoding=encoding, errors='ignore') as f:
content = []
count = count_line(file_name,rep_word)
for eachline in f:
if rep_word in eachline:
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n文件 %s 中共有%s个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:'
% (file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
with open(file_name, 'w') as f:
f.writelines(content)
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)
能看下这个代码有什么问题吗
页:
[1]