Extract part of file and print using python
<p>I want to extract part of the file content . Which starts with ("head"
and ends with ("arraydat"</p>

<p>content of file:</p>

<pre><code>(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)
        )
</code></pre>

<p>I need to extract the content of a file after "head" and before "arraydat". 
 I have tried this command but no luck </p>

<pre><code>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)
</code></pre>

<p>Output should look like this:</p>

<pre><code>    ( "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)
       )
     )
</code></pre>

<p>Please check the below code:</p>

<pre><code>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)
</code></pre>

<p><strong>Output :</strong></p>

<p><em>On console:</em></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;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&gt;
</code></pre>

<p><em>Content of <strong>output.csv</strong>:</em></p>

<pre><code>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
</code></pre>

python