用python如何在文本指定位置插入内容
Admin 2022-12-24 群英技术资讯 898 次浏览
关于“用python如何在文本指定位置插入内容”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。fp = open('D://代码开发//Python.path//jhp//fadd.txt', 'r') #指定文件
s = fp.read() #将指定文件读入内存
fp.close() #关闭该文件
a = s.split('\n')
a.insert(-1, 'a new line') #在第 LINE+1 行插入
s = '\n'.join(a) #用'\n'连接各个元素
fp = open('D://代码开发//Python.path//jhp//fadd.txt', 'w')
fp.write(s)
fp.close()
结果:
"properties":{
"zookeeper.connect":"zookeeper.com:2015",
"druid.discovery.curator.path":"/druid/discovery",
"druid.selectors.indexing.serviceName":"druid/overlord",
"commit.periodMillis":"12500",
"consumer.numThreads":"1",
"kafka.zookeeper.connect":"kafkaka.com:2181,kafka.com:2181,kafka.com:2181",
"kafka.group.id":"test_dataSource_hod_dd"
a new line
}
生产环境需要对大量的json文件进行写操作,在指定节点中插入一个属性。如下:
{
"dataSources":{
"test_dataSource_hod":{
"spec":{
"dataSchema":{
"dataSource":"test_dataSource_hod",
"parser":{
"type":"string",
"parseSpec":{
"timestampSpec":{
"column":"timestamp",
"format":"yyyy-MM-dd HH:mm:ss"
},
"dimensionsSpec":{
"dimensions":[
"method",
"key"
]
},
"format":"json"
}
},
"granularitySpec":{
"type":"uniform",
"segmentGranularity":"hour",
"queryGranularity":"none"
},
"metricsSpec":[
{
"name":"count",
"type":"count"
},
{
"name":"call_count",
"type":"longSum",
"fieldName":"call_count"
},
{
"name":"succ_count",
"type":"longSum",
"fieldName":"succ_count"
},
{
"name":"fail_count",
"type":"longSum",
"fieldName":"fail_count"
}
]
},
"ioConfig":{
"type":"realtime"
},
"tuningConfig":{
"type":"realtime",
"maxRowsInMemory":"100000",
"intermediatePersistPeriod":"PT10M",
"windowPeriod":"PT10M"
}
},
"properties":{
"task.partitions":"1",
"task.replicants":"1",
"topicPattern":"test_topic"
}
}
},
"properties":{
"zookeeper.connect":"zookeeper.com:2015",
"druid.discovery.curator.path":"/druid/discovery",
"druid.selectors.indexing.serviceName":"druid/overlord",
"commit.periodMillis":"12500",
"consumer.numThreads":"1",
"kafka.zookeeper.connect":"kafkaka.com:2181,kafka.com:2181,kafka.com:2181",
"kafka.group.id":"test_dataSource_hod_dd"
}
}
需要在最后的properties节点中添加一个"druidBeam.randomizeTaskId":"true"属性。
大概的思路如下:
我觉得稍微有点难的地方是在确认插入位置的地方。我们知道的是"druid.selectors.indexing.serviceName":"druid/overlord",这个东西肯定在这个节点中,那我只要能找到这个东西,然后在他的后面 插入就OK了。
好了,思路已经有了,写代码吧。
#!/usr/bin/python
# coding:utf-8
import os
old_string = '"druid/overlord"'
new_string = ('"druid/overlord",' +
'\n ' +
'"druidBeam.randomizeTaskId":"true",')
def insertrandomproperty(file_name):
if '.json' in file_name:
with open(file, 'r') as oldfile:
content = oldfile.read()
checkandinsert(content, file)
else:
pass
def checkandinsert(content, file):
if 'druidBeam.randomizeTaskId' not in content:
# to avoid ^M appear in the new file because of different os
# we replace \r with ''
new_content = content.replace(old_string, new_string).replace('\r', '')
with open(file, 'w') as newfile:
newfile.write(new_content)
else:
pass
if __name__ == '__main__':
files = os.listdir('/home/tranquility/conf/service_bak')
os.chdir('/home/tranquility/conf/service_bak')
for file in files:
insertrandomproperty(file)
就是在内存中更新内容,然后重新写回到文件中。代码只是粗略的表达了思路,可以根据需求继续修改优化。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了pytorch部署到jupyter中,在这里需要注意我再输入的时候出现了一些无法定位的提示,但是我的电脑没有影响使用jupyter,还是可以使用jupyter并且可以import torch,本文给大家讲解的非常详细,需要的朋友参考下吧
因为Python是脚本语言,不会进行编译,所以只有执行到那一行,才能知道那个变量的类型,下面这篇文章主要给大家介绍了关于pycharm查看变量值的4种方法,需要的朋友可以参考下
这篇文章主要介绍了Python 如何查看程序内存占用情况,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了python计算机视觉OpenCV入门讲解,关于图像处理的相关简单操作,包括读入图像、显示图像及图像相关理论知识
这篇文章主要介绍了Python字符串编码转换 encode()和decode()方法详细的说明,下面文章围绕encode()和decode()方法的相相关资料展开内容,具有一定的价值,需要的朋友卡通参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008