2023-06-11 23:57:58 +02:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2023-06-12 00:59:19 +02:00
|
|
|
"execution_count": 4,
|
2023-06-11 23:57:58 +02:00
|
|
|
"metadata": {},
|
2023-06-12 00:59:19 +02:00
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
|
|
|
"Data has been formatted and saved to output.txt\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2023-06-11 23:57:58 +02:00
|
|
|
"source": [
|
|
|
|
"import re\n",
|
|
|
|
"\n",
|
|
|
|
"# Open input file in read mode and output file in write mode\n",
|
2023-06-12 00:59:19 +02:00
|
|
|
"with open('test-A/out.tsv', 'r', encoding=\"utf-8\") as infile, open('test-A/parsed.tsv', 'w', encoding=\"utf-8\") as outfile:\n",
|
|
|
|
"\n",
|
|
|
|
" # Default text to use when text segment is missing\n",
|
|
|
|
" default_text = \"Airconditioner\"\n",
|
|
|
|
"\n",
|
2023-06-11 23:57:58 +02:00
|
|
|
" # Iterate through each line in the input file\n",
|
|
|
|
" for line in infile:\n",
|
2023-06-12 00:59:19 +02:00
|
|
|
"\n",
|
2023-06-11 23:57:58 +02:00
|
|
|
" # Regular expression to match \"{...}\" or \"}\"\n",
|
2023-06-12 00:59:19 +02:00
|
|
|
" match = re.search(r'({.*}|\\}[^{]*$)', line)\n",
|
|
|
|
"\n",
|
2023-06-11 23:57:58 +02:00
|
|
|
" # If \"{...}\" or \"}\" is found\n",
|
|
|
|
" if match:\n",
|
|
|
|
" # Extract \"{...}\" or \"}\" from the line\n",
|
2023-06-12 00:59:19 +02:00
|
|
|
" data = match.group(0).strip()\n",
|
|
|
|
"\n",
|
2023-06-11 23:57:58 +02:00
|
|
|
" # Extract the text before \"{...}\" or \"}\"\n",
|
|
|
|
" text = line[:match.start()].strip()\n",
|
2023-06-12 00:59:19 +02:00
|
|
|
"\n",
|
|
|
|
" # If data is not enclosed within curly braces, enclose it\n",
|
|
|
|
" if not data.startswith('{'):\n",
|
|
|
|
" data = '{' + data\n",
|
|
|
|
"\n",
|
|
|
|
" # If text segment is missing, use default text\n",
|
|
|
|
" if not text:\n",
|
|
|
|
" text = default_text\n",
|
|
|
|
"\n",
|
|
|
|
" # Write to output file with the desired format\n",
|
|
|
|
" outfile.write(f\"{text}\\t{text}\\t{data}\\n\")\n",
|
2023-06-11 23:57:58 +02:00
|
|
|
" else:\n",
|
|
|
|
" # If the line doesn't contain \"{...}\" or \"}\", write it as is\n",
|
|
|
|
" outfile.write(line)\n",
|
|
|
|
"\n",
|
|
|
|
"# Done!\n",
|
|
|
|
"print(\"Data has been formatted and saved to output.txt\")\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "modelowanie",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
2023-06-12 00:59:19 +02:00
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
2023-06-11 23:57:58 +02:00
|
|
|
"name": "python",
|
2023-06-12 00:59:19 +02:00
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2023-06-11 23:57:58 +02:00
|
|
|
"version": "3.10.10"
|
|
|
|
},
|
|
|
|
"orig_nbformat": 4
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 2
|
|
|
|
}
|