{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Processing Notebook\n", "\n", "The purpose of this notebook is to help me process the RAW S3 data into the right format to upload to Hugging Face." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import dependencies\n", "import os\n", "from datetime import datetime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Make directory called `data/faa` if it doesn't already exist.\n", "if not os.path.exists('data/faa'):\n", "\tos.makedirs('data/faa')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# For each file in `s3_data` directory\n", "xml_data_strings = []\n", "for file in os.listdir('s3_data'):\n", "\t# If file doesn't end in `.txt`, continue\n", "\tif not file.endswith('.txt'):\n", "\t\tcontinue\n", "\t# Open the file in read mode\n", "\twith open(f's3_data/{file}', 'r') as f:\n", "\t\t# Read the file content\n", "\t\tcontent = f.read()\n", "\n", "\t\t# Split the content by the separator\n", "\t\tparts = content.split('<><><><><>')\n", "\n", "\t\txml_data = ''\n", "\t\t# Get the XML data as a string\n", "\t\tif len(parts) == 1:\n", "\t\t\txml_data_strings.append(parts[0].split('---')[1].strip())\n", "\t\telse:\n", "\t\t\tfor part in parts:\n", "\t\t\t\txml_data_strings.append(part.split('---')[1].strip())\n", "\n", "# Delete any duplicate strings in the xml_data_strings list\n", "xml_data_strings = list(set(xml_data_strings))\n", "\n", "# Sort the XML data strings by the `Wed Jul 10 17:18:22 2024 GMT` date\n", "def extract_update_time(xml_string):\n", "\tstart = xml_string.find(\"\") + len(\"\")\n", "\tend = xml_string.find(\"\")\n", "\tdate_str = xml_string[start:end].strip()\n", "\treturn datetime.strptime(date_str, \"%a %b %d %H:%M:%S %Y %Z\")\n", "xml_data_strings.sort(key=extract_update_time)\n", "\n", "# Write the XML data to a single file in the `data/faa` directory with each XML data string on a new line\n", "with open('data/faa/faa_xml_data.csv', 'w') as f:\n", "\tfor xml_data in xml_data_strings:\n", "\t\tf.write(xml_data + '\\n')\n", "\n", "# Delete the xml_data_strings list to free up memory\n", "del xml_data_strings" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" } }, "nbformat": 4, "nbformat_minor": 2 }