233倔强不秃 发表于 2020-2-22 00:31:06

文件中替换字符串

"""replace strings in text"""

import os

def Replace(file_name, rep_word, new_word):
    with open(file_name) as f:
      content = []
      count = 0

      for eachline in f:
            if rep_word in eachline:
                count += eachline.count(rep_word)
                eachline = eachline.replace(rep_word, new_word)
            content.append(eachline)

      decide = input('文件 {0} 中共有{1}个【{2}】\n您确定要把所有的【{3}】替换为【{4}】吗?\n【YES/NO】:'.format\
                (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)
            print('Succeed!')
      else:
            print('Exit!')

if __name__ == '__main__':
    while True:
      file_name = input('请输入文件名:')

      if file_name in os.listdir():
            rep_word = input('请输入需要替换的单词或字符:')
            new_word = input('请输入新的单词或字符:')
            Replace(file_name, rep_word, new_word)
            break
      else:
            print('Do not find such a file {}'.format(file_name))
页: [1]
查看完整版本: 文件中替换字符串