btdparse-python/parse_transducer.py
2023-07-21 22:35:49 -04:00

69 lines
2.7 KiB
Python

import re
def parse_transducer(file_path):
transducer_data = []
with open(file_path, 'r') as file:
content = file.read()
# Split the content into sections based on the blank line
sections = content.strip().split('\n\n')
for section in sections:
# Split each section into lines
lines = section.strip().split('\n')
lines = [line.strip() for line in lines if not line.startswith("==") and line != "|| Transducer Verify Report ||"]
lines.pop(0)
# Extract the Transducer number and Transducer type
transducer_line = lines.pop(0).strip()
_, transducer_name, transducer_type = transducer_line.split(None, 2)
# Create a dictionary to store the data for each transducer
transducer_info = {
'Transducer Name': transducer_name,
'Transducer Type': transducer_type,
'Setpoint Pressures': {},
'Instrument Pressures': {},
'Master Values': {},
'Instrument Flows': {},
'Verify Date': '',
'Verify Time': ''
}
# Extract other information for the transducer
for line in lines:
key, value = re.sub(r'\s\s+', ',', line.strip()).split(',')
if 'Setpoint Pressure' in key:
transducer_info['Setpoint Pressures'][key] = value
elif 'Instrument Pressure' in key:
transducer_info['Instrument Pressures'][key] = value
elif 'Master Value' in key:
transducer_info['Master Values'][key] = value
elif 'Instrument Flow' in key:
transducer_info['Instrument Flows'][key] = value
elif 'Verify Date' in key:
transducer_info['Verify Date'] = value
elif 'Verify Time' in key:
transducer_info['Verify Time'] = value
transducer_data.append(transducer_info)
return transducer_data
if __name__ == '__main__':
file_path = './transducer_verify.txt'
parsed_data = parse_text_file(file_path)
# Printing the parsed data for each transducer
for transducer_info in parsed_data:
print("Transducer Name:", transducer_info['Transducer Name'])
print("Transducer Type:", transducer_info['Transducer Type'])
print("Setpoint Pressures:", transducer_info['Setpoint Pressures'])
print("Instrument Pressures:", transducer_info['Instrument Pressures'])
print("Master Values:", transducer_info['Master Values'])
print("Instrument Flows:", transducer_info['Instrument Flows'])
print("Verify Date:", transducer_info['Verify Date'])
print("Verify Time:", transducer_info['Verify Time'])
print("==========================")