Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取部分文件并使用python打印_Python - Fatal编程技术网

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

提取部分文件并使用python打印,python,Python,我想提取部分文件内容。以“头”开头 并以(“arraydat”结尾 文件内容: (record ( "head" (record ( "pname" "C16D") ( "ptype" "probe") ( "sn" "11224") ( "rev" "1") ( "opname" "Kaji") ( "comm" [ "" ] ) ( "numel

我想提取部分文件内容。以“头”开头 并以(“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)
       )
     )

请检查以下代码:

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)
输出:

在控制台上:

output.csv的内容


我希望这能有所帮助。

谢谢你的解决方案。它工作得很好。你能帮我写代码吗?行的第一部分是csv文件的头,第二部分是数据。例如,如果(“pname”“C16D”)是内容,我想把“pname”作为csv文件的头,“C16D”作为标题下的数据,其余行的处理过程类似。谢谢。我也希望将此内容放入csv文件。其中第一部分作为标题,第二部分作为其下的数据。例如,如果(“pname”“C16D”)是内容,我想将“pname”作为csv文件的标题和“C16D”作为该标题下的数据,其余行的处理过程类似。我想你知道我的预期输出是什么:)不,这不起作用。我能得到一个优化的解决方案吗。只有标题部分出现在输出文件.csv中,但数据没有进入csvDinesh。我得到了这个错误回溯(最近一次调用):文件“Dinesh.py”,第60行,在writer.writerow(数据)文件“C:\Python33\lib\csv.py”中,第153行,在writerow中返回self.writer.writerow(self.dict\u to_list(rowdict))文件“C:\Python33\lib\csv.py”,第149行,在_dict\u to_list+,”中。连接(错误字段)值错误:dict包含的字段不在字段名中:numrow、numel、date、ptype、comm、mux、PartNum、,maker、rev、opname、focus、probID、pname、Polarial、sn、freq、te、Thermal数据列表[0]。keys()仅显示MWK YMS托盘探头TMX VND
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>
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
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)