使用Python读取txt中的指定内容

发布时间 2023-07-24 11:52:42作者: 冥天肝

一、序

在一些情况下,导出txt的配置信息,但是又有特定的规律。在这种情况下读取配置

二、问题分析

我们首先肯定是通过关键字定位文本位置,但txt文件我们会面临两种情况:
1.关键字与文本在同一行

2.关键字与文本不在同一行

3.判断条件
例如MAC地址只有有IP的才读取,没有IP的就不读取

三、解决思路

1.关键字与文本在同一行情况
 
     def sameLine(txt_Path, needWord):
        with open(txt_Path, "r") as file:
            words = needWord
            returnWord = []
            for line in file.readlines():
                if line.strip().startswith(words):
                    returnWord.append(line.strip().replace(words, ""))
        if len(returnWord) == 0:
            returnWord.append("无")
        return returnWord
  </code></pre>
2.关键字与文本不在同一行情况
 
      # 得到指定关键字行数
        def readTxtResult(txt_Path, needWord):
            with open(txt_Path, "r") as file:
                row = []
                words = needWord
                for i, line in enumerate(file.readlines(), start=1):
                    if words in line.strip():
                        b: int = i
                        row.append(b)
                    else:
                        var = ()
                return row
    # 得到指定一行的文本
    def getRowWord(txt_Path, row):
        with open(txt_Path, "r") as file:
            lines = file.readlines()
            word = lines[row].strip()
            return word
  </code></pre>
  1. 如果是有判断条件,先去需要的地方获取判断条件,自定义判断条件
3.总代码
 
    import os.path
    import re


    def extract_chinese(text):
        if '0' in text:
            text = text.strip('0')
        pattern = re.compile(r'[\u4e00-\u9fa5]+')
        result = pattern.findall(text)
        return ''.join(result)


    def listToStr(strList):
        strs = ""
        if len(strList) > 0:
            for i in strList:
                strs = strs + i
                if i != strList[-1]:
                    strs = strs + "\\"
        return strs


    def returnNo(txt_Path):
        # 获取部门
        parent_dir = os.path.dirname(txt_Path)
        dep = os.path.basename(parent_dir)
        if '0' in dep:
            dep = dep.strip('0')
        # 获取编号
        name = os.path.basename(txt_Path)
        name = name.replace('.txt', '')

        userNo: str = re.sub('[\u4e00-\u9fa5]', '', name)
        name = extract_chinese(name)

        return dep, userNo, name


    # 得到指定关键字行数
    def readTxtResult(txt_Path, needWord):
        with open(txt_Path, "r") as file:
            row = []
            words = needWord
            for i, line in enumerate(file.readlines(), start=1):
                if words in line.strip():
                    b: int = i
                    row.append(b)
                else:
                    var = ()
            return row


    def sameLine(txt_Path, needWord):
        with open(txt_Path, "r") as file:
            words = needWord
            returnWord = []
            for line in file.readlines():
                if line.strip().startswith(words):
                    returnWord.append(line.strip().replace(words, ""))
        if len(returnWord) == 0:
            returnWord.append("无")
        return returnWord


    # 得到指定一行的文本
    def getRowWord(txt_Path, row):
        with open(txt_Path, "r") as file:
            lines = file.readlines()
            word = lines[row].strip()
            return word


    def if_SSD(word):
        if "SSD" in word:
            return "(SSD)"
        else:
            return "(SATA)"


    def toGBorDB(lists):
        for i in range(0, len(lists)):
            num_str = lists[i].strip("字节")
            lists[i] = num_str.strip("\t")
            numtoTB = lists[i]
            conunt_str = lists[i].count(",")
            if conunt_str >= 4:
                lists[i] = ''.join(numtoTB.split())[:-16].upper() + 'TB'

            elif conunt_str == 3:
                lists[i] = ''.join(numtoTB.split())[:-12].upper() + 'GB'

        return lists


    def readAll(txt_Path):
        try:
            readList = [returnNo(txt_Path)[0], returnNo(txt_Path)[1]]
            PcType = sameLine(txt_Path, "电脑类型:")[0]
            readList.append(PcType.strip(" "))

            row_OS = readTxtResult(txt_Path, "操作系统")
            Pc_OS = getRowWord(txt_Path, row_OS[0])
            readList.append(Pc_OS)

            row_MAC = readTxtResult(txt_Path, "MAC地址")
            Pc_MAC_list = []
            Pc_IP_list = []
            if len(row_MAC) > 0:
                for i in row_MAC:
                    if 'IP地址' in getRowWord(txt_Path, i):
                        Pc_MAC = getRowWord(txt_Path, i - 1).strip('MAC地址')
                        Pc_MAC_list.append(str(Pc_MAC).strip("\t"))
                        Pc_IP = getRowWord(txt_Path, i).strip("IP地址")
                        Pc_IP_list.append(Pc_IP.strip('\t'))
            # 判断是否上网
            if len(Pc_IP_list) > 0:
                PC_Net = "是"
            else:
                PC_Net = "否"
            readList.append(listToStr(Pc_IP_list))
            readList.append(listToStr(Pc_MAC_list))

            Pc_CPU = sameLine(txt_Path, "规格")
            row_RAM = readTxtResult(txt_Path, "RAM")
            Pc_RAM = getRowWord(txt_Path, row_RAM[0]).split('GB', 1)[0] + "GB"

            Pc_SSD = sameLine(txt_Path, "真实大小")
            Pc_SSD = toGBorDB(Pc_SSD)
            SSD_Num = len(Pc_SSD)
            row_SSD = readTxtResult(txt_Path, "存储器")[0]
            Pc_SSDType = []
            for i in range(0, SSD_Num):
                Pc_SSDType.append(if_SSD(getRowWord(txt_Path, row_SSD)))

            Pc_allSSD = f"{Pc_SSD[0]}" + f"{Pc_SSDType[0]}"
            if SSD_Num > 1:
                for i in range(1, SSD_Num):
                    Pc_allSSD = Pc_allSSD + f"+{Pc_SSD[i]}" + f"{Pc_SSDType[i]}"

            Pc_allSSD = Pc_CPU[0].strip("\t") + "\n" + Pc_RAM + "\n" + Pc_allSSD
            readList.append(Pc_allSSD)
            readList.append(PC_Net)
            readList.append(returnNo(txt_Path)[2])
            return readList
        except Exception as e:
            print(e)


    if __name__ == '__main__':
        # r不用管\ ,f可以使用变量
        path = r"测试.txt"