Question: import csv import re # Define the input and output file names and paths baselight _ file = / Users / alex / Documents

import csv
import re
# Define the input and output file names and paths
baselight_file ="/Users/alex/Documents/pythonProjects/Baselight_export.txt"
xytech_file ="/Users/alex/Documents/pythonProjects/Xytech.txt"
output_file = "Output.csv"
# Define the field names for the CSV file
fieldnames =["Producer", "Operator", "Job", "Notes", "Location", "Frames"]
# Define a function to parse the data from the text files using regular expressions
def parse_data(baselight_file, xytech_file):
# Create an empty list to store the data
data =[]
# Open the baselight file and read the lines
with open(baselight_file) as f:
lines = f.readlines()
# Loop through the lines and extract the location and frames
for line in lines:
# Skip the empty lines
if line.strip():
# Use regular expressions to match the location and frames
match = re.search(r"(/.+/)(\d+)(\s+.+)", line)
if match:
# Get the location and frames from the match object
location = match.group(1)
frames = match.group(3)
# Append the location and frames to the data list
data.append([None, None, None, None, location, frames])
# Open the xytech file and read the lines
with open(xytech_file) as f:
lines = f.readlines()
# Initialize variables to store Xytech data
producer = None
operator = None
job = None
notes = None
location = None
frames = None
# Loop through the lines and extract the producer, operator, job, notes, and location
for line in lines:
# Skip the empty lines
if line.strip():
# Use regular expressions to match the producer, operator, job, notes, and location
match = re.search(r"(\w+):\s+(.+)", line)
if match:
# Get the key and value from the match object
key = match.group(1)
value = match.group(2)
# Assign values to corresponding variables
if key == "Producer":
producer = value
elif key == "Operator":
operator = value
elif key == "Job":
job = value
elif key == "Notes":
notes = value
elif key == "Location":
location = value
# If all required fields are populated, append the data to the list
if producer and operator and job and notes and location and frames:
data[-1][:4]=[producer, operator, job, notes]
# Reset variables for next entry
producer = None
operator = None
job = None
notes = None
location = None
frames = None
# Return the data list
return data
# Define a function to export the data to a CSV file using csv module
def export_data(data, output_file, fieldnames):
# Open the output file in write mode
with open(output_file, "w", newline='') as f:
# Create a csv writer object
writer = csv.writer(f)
# Write the field names as the header row
writer.writerow(fieldnames)
# Write the data rows
writer.writerows(data)
# Call the functions and write the output to the CSV file
data = parse_data(baselight_file, xytech_file)
export_data(data, output_file, fieldnames) it works but its not showing anything other than Producer Operator Job Notes Location Frames

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!