網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
如何在文件中部插入信息
fp = open('D://代碼開(kāi)發(fā)//Python.path//jhp//fadd.txt', 'r') #指定文件
s = fp.read() #將指定文件讀入內(nèi)存
fp.close() #關(guān)閉該文件
a = s.split('\n')
a.insert(-1, 'a new line') #在第 LINE+1 行插入
s = '\n'.join(a) #用'\n'連接各個(gè)元素
fp = open('D://代碼開(kāi)發(fā)//Python.path//jhp//fadd.txt', 'w')
fp.write(s)
fp.close()
結(jié)果:
"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
? ? }
實(shí)現(xiàn)在文本指定位置插入內(nèi)容
1. 場(chǎng)景
生產(chǎn)環(huán)境需要對(duì)大量的json文件進(jìn)行寫(xiě)操作,在指定節(jié)點(diǎn)中插入一個(gè)屬性。如下:
{
"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節(jié)點(diǎn)中添加一個(gè)"druidBeam.randomizeTaskId":"true"屬性。
2. 思路
大概的思路如下:
- 掃描文件夾下所有需要更改的文件
- 在文件中確認(rèn)需要更改的位置
- 插入新的字符
我覺(jué)得稍微有點(diǎn)難的地方是在確認(rèn)插入位置的地方。我們知道的是"druid.selectors.indexing.serviceName":"druid/overlord",這個(gè)東西肯定在這個(gè)節(jié)點(diǎn)中,那我只要能找到這個(gè)東西,然后在他的后面 插入就OK了。
好了,思路已經(jīng)有了,寫(xiě)代碼吧。
#!/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)
就是在內(nèi)存中更新內(nèi)容,然后重新寫(xiě)回到文件中。代碼只是粗略的表達(dá)了思路,可以根據(jù)需求繼續(xù)修改優(yōu)化。
原文鏈接:https://blog.csdn.net/weixin_42891455/article/details/121952348
相關(guān)推薦
- 2022-11-06 Android?Navigation重建Fragment問(wèn)題分析及解決_Android
- 2023-02-14 Cython處理C字符串的示例詳解_python
- 2022-09-30 Python3中map()、reduce()、filter()的用法詳解_python
- 2022-10-11 RabbitMQ:生產(chǎn)者消息確認(rèn)、消息持久化、消費(fèi)者消息確認(rèn)、消費(fèi)失敗重試機(jī)制
- 2022-05-06 dyld[59644]: Library not loaded: /usr/local/opt/ic
- 2022-09-06 C#任務(wù)并行Parellel.For和Parallel.ForEach_C#教程
- 2022-09-02 C語(yǔ)言求階乘之和的三種實(shí)現(xiàn)方法(先階乘再累加)_C 語(yǔ)言
- 2021-12-07 Linux下Hbase安裝配置教程_Linux
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支