zoukankan      html  css  js  c++  java
  • python 新手题目-文件读写-关键字判断

    import  os
    import json
    import re
    
    filename ="D:/pytest/2000/100/111.txt"
    
    f = open(filename,"r",encoding="gbk")
    f1 = open("tmpfile","w",encoding="utf-8")
    lock ="False" # 表示开始匹配位置
    status = "False" #默认为 1
    tmp_str =''
    
    
    for line in f:
        tmp_line =line.strip()
         #开始位置,  and lock == 'False':
        if "{" in line  and lock == 'False':
             lock ='True'
             print("开始插入位置",line )
             tmp_str =""
             tmp_str = tmp_str + "
    " + line #将开始加入到字符串
             continue
    
        elif "{" in line  and lock == 'True':
             print("{重复,去掉之前数据")
             lock = 'True'
             tmp_str = ""
             tmp_str = tmp_str + "
    " + line  #
             continue
        else :
            pass
    
         # 结束位置
        if "}" in line and lock == 'True':
             print("结束位置")
             lock='False'
             tmp_str = tmp_str+"
    "+line
             if  status == "True" : #结束时候判断是否写入。
                 print("开始写入",tmp_str)
                 f1.write(tmp_str)
             print("test")
             tmp_str ='' #重新清空
             continue
    
    
    
         #不是开始位置也不是结束位置。开始循环
        if  lock == 'True':
             tmp_str=tmp_str +tmp_line
             if "Status" in tmp_line:
                 list = re.split('[":,]', tmp_line)  # list[4]
                 print("开始判断",list[4])
                 if list[4] == "1":
                     status = "False" #成功不需要保留
                     print("不需要保留")
                 elif list[4] == "2":
                     status = "True" #失败需要保留
                     print("需要写入数据",status,lock,tmp_str,tmp_line)
                 else:
                     print("其他错误")
                     status ='False'
             continue
    
    f.close()
    f1.close()
  • 相关阅读:
    phonegap helloworld 之android
    perl 信号
    css的浮动
    css的定位
    p4 环境变量的优先级
    oracle sql 高级
    perl数组高级
    让你提升命令行效率的 Bash 快捷键 [完整版]
    函数的返回值为结构体类型
    函数的返回值保存在内存的什么区域
  • 原文地址:https://www.cnblogs.com/xiajq/p/9519292.html
Copyright © 2011-2022 走看看