提取部分文件并使用python打印

2024-05-08 23:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想提取部分文件内容。以“头”开头 以“arraydat”结尾

文件内容:

(record
  ( "head"
    (record
     ( "pname" "C16D")
     ( "ptype" "probe")
       ( "sn" "11224")
         ( "rev" "1")
          ( "opname" "Kaji")
         ( "comm"
         [ "" ]
        )
      ( "numel" 192)
      ( "mux" 1)
      ( "freq" 3400000)
     ( "date" 63602497416.093)
      ( "focus" 0.066)
      ( "probID" 574)
          ( "te" 0)
     ( "therm" 0)
      ( "bipolar" "N/A")
      ( "maker" "YMS")
     ( "PartNum" 5418916)
       ( "numrow" 1)
       )
      )
     ( "arraydat"
       (record
      ( "FL6" 1625283.947393933)
      ( "FH6" 4932875.254089763)
     ( "FL20" 1283607.261269079)
     ( "FH20" 5673248.882271254)
     ( "Fc" 3279079.600741847)
      ( "BW" 100.8695033187829)
      ( "PW6" 3.316821935120381E-007)
    ( "PW20" 9.740000000000003E-007)
       ( "PW30" 1.456E-006)
       ( "PW40" 2.628000000000001E-006)
       ( "LG" -46.35823409318354)
       ( "TOF" -1.363659434369523E-008)
        )

我需要在“head”之后和“arraydat”之前提取文件的内容。 我试过这个命令,但运气不好

import re
    with open('sample.txt','r') as new_file:
    data = new_file.read()
    pattern = re.compile(r'\s[(]\s"head"[\s\S]*?\s[(]\s"arraydat"')
    stringtext = re.findall(pattern, data)
    print(stringtext)

输出应如下所示:

    ( "pname" "C16D")
  ( "ptype" "probe")
     ( "sn" "11224")
    ( "rev" "1")
   ( "opname" "Kaji")
      ( "comm"
         [ "" ]
        )
      ( "numel" 192)
      ( "mux" 1)
      ( "freq" 3400000)
     ( "date" 63602497416.093)
      ( "focus" 0.066)
      ( "probID" 574)
          ( "te" 0)
     ( "therm" 0)
      ( "bipolar" "N/A")
      ( "maker" "YMS")
     ( "PartNum" 5418916)
       ( "numrow" 1)
       )
     )

Tags: 文件re内容revrecordheadprobecomm
3条回答

请检查以下代码:

import re
import csv

data_list= []
record_data = False
comm_line = False

#Open file read data.
#Save one set of data between 'head' and 'arraydat' in single dict
#Append that dict to data list

with open('sample.txt','r') as new_file:
     for line in new_file:
          if '( "head"' in line:
              record_data  = True
              data_dict = {}
              continue

          if '( "arraydat"' in line:
              data_list.append(data_dict)
              record_data = False

          #Data from comm line  
          if "comm" in line and record_data:
              comm_line = True
              nline = ''

          if comm_line:
              n = re.match(r'\s*\)\s*$',line)
              if n is not None:
                comm_line=False
                nline = nline + line.strip('\r\n')
                line=re.sub(' +',' ',nline)
                n = None
              else:
                nline = nline + line.strip('\r\n')
                next

          if record_data:
             line  = line.strip()
             if line.startswith('(') and line.endswith(')'):
                  line = line.strip(')(').strip()
                  #line = re.sub('\"',"",line)
                  #print line
                  m = re.match(r'\"(\w+)\"\s+\"*([\w+\W+]+)\"*',line)
                  if m is not None:
                       k = m.group(1).strip('"')
                       v  = m.group(2).strip('"')
                       data_dict[k]=v

print data_list

#Write it to csv
with open('output.csv','wb') as out_file:
     writer = csv.DictWriter(out_file, fieldnames=data_list[0].keys())
     writer.writeheader()
     for data in data_list:
          writer.writerow(data)

输出:

控制台上:

C:\Users\dinesh_pundkar\Desktop>python b.py
[{'therm': '0', 'probID': '574', 'PartNum': '5418916', 'numrow': '1', 'rev': '1'
, 'ptype': 'probe', 'mux': '1', 'bipolar': 'N/A', 'maker': 'YMS', 'comm': '[ ""
]', 'sn': '11224', 'numel': '192', 'focus': '0.066', 'date': '63602497416.093',
'pname': 'C16D', 'opname': 'Kaji', 'te': '0', 'freq': '3400000'}, {'therm': '0',
 'probID': '574', 'PartNum': '5418916', 'numrow': '1', 'rev': '1', 'ptype': 'pro
be', 'mux': '1', 'bipolar': 'N/A', 'maker': 'YMS', 'comm': '[ "" ]', 'sn': '1122
4', 'numel': '192', 'focus': '0.066', 'date': '63602497416.093', 'pname': 'C16D'
, 'opname': 'Dinesh', 'te': '0', 'freq': '3400000'}]

C:\Users\dinesh_pundkar\Desktop>

内容输出.csv

therm,probID,PartNum,numrow,rev,ptype,mux,bipolar,maker,comm,sn,numel,focus,date,pname,opname,te,freq
0,574,5418916,1,1,probe,1,N/A,YMS,"[ """" ]",11224,192,0.066,63602497416.093,C16D,Kaji,0,3400000
0,574,5418916,1,1,probe,1,N/A,YMS,"[ """" ]",11224,192,0.066,63602497416.093,C16D,Dinesh,0,3400000
data = []
read = False

for line in open('test.dat'):
    if line.strip() == '( "head"':
        read = True
        continue
    elif line.strip() == '( "arraydat"':
        read = False
    if read:
        data.append(line.rstrip())

print('\n'.join(data))
try:
    with open('sample.txt', 'r') as fileObject:
        fileData = fileObject.read().split("\n")

    fileDataClean = [data.strip() for data in fileData]

    startIndex, stopIndex = fileDataClean.index('( "head"'), fileDataClean.index('( "arraydat"')
    resultData = "\n"join(fileData[startIndex:stopIndex])
    print(resultData)

except Exception as e:
    print(e)

我希望这有帮助。

相关问题 更多 >