阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

简单介绍python如何在文件中部插入信息

55次阅读
没有评论

共计 2973 个字符,预计需要花费 8 分钟才能阅读完成。

导读 这篇文章主要介绍了 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
    }
实现在文本指定位置插入内容
1. 场景

生产环境需要对大量的 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” 属性。

2. 思路

大概的思路如下:

  • 扫描文件夹下所有需要更改的文件
  • 在文件中确认需要更改的位置
  • 插入新的字符
  • 我觉得稍微有点难的地方是在确认插入位置的地方。我们知道的是 ”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)

    就是在内存中更新内容,然后重新写回到文件中。代码只是粗略的表达了思路,可以根据需求继续修改优化。

    阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

    腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

    代金券:在阿里云专用满减优惠券

    正文完
    星哥说事-微信公众号
    post-qrcode
     0
    星锅
    版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计2973字。
    转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
    【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
    阿里云-最新活动爆款每日限量供应
    评论(没有评论)
    验证码
    【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中