python 替换文本中指定内容

主要用到了 replace(a,b) 函数,将a替换为b。

#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : [email protected]
#   File Name     : replace.py
#   Last Modified : 2020-03-09 17:14
#   Description   :
#
# ====================================================

import sys

def lm_replace():
	f = open("./test.fs","r+")
	content_before = f.read()
	print(content_before)
	# 将blendNormal 替换为 blendScreen
	content_after = content_before.replace('blendNormal',"blendScreen")
	print(content_after)
	f.seek(0,0)
	f.truncate()	#清空文件,配合seek使用,否则清空的位置不对
	f.write(content_after)
	f.close()

猜你喜欢

转载自blog.csdn.net/zxcasd11/article/details/105201504