solution
This commit is contained in:
parent
ad39bd5180
commit
bc5869a233
10677
dev-A/out.tsv
Normal file
10677
dev-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
10677
dev-A/raw-out.tsv
Normal file
10677
dev-A/raw-out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
63
format.ipynb
Normal file
63
format.ipynb
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"\n",
|
||||
"# Open input file in read mode and output file in write mode\n",
|
||||
"with open('dev-A/out.tsv', 'r', encoding=\"utf-8\") as infile, open('dev-A/parsed.tsv', 'w', encoding=\"utf-8\") as outfile:\n",
|
||||
" \n",
|
||||
" # Iterate through each line in the input file\n",
|
||||
" for line in infile:\n",
|
||||
" \n",
|
||||
" # Regular expression to match \"{...}\" or \"}\"\n",
|
||||
" match = re.search(r'({.*}|\\})', line)\n",
|
||||
" \n",
|
||||
" # If \"{...}\" or \"}\" is found\n",
|
||||
" if match:\n",
|
||||
" # Extract \"{...}\" or \"}\" from the line\n",
|
||||
" data = match.group(0)\n",
|
||||
" \n",
|
||||
" # Extract the text before \"{...}\" or \"}\"\n",
|
||||
" text = line[:match.start()].strip()\n",
|
||||
" \n",
|
||||
" # Split the text into segments\n",
|
||||
" segments = text.split()\n",
|
||||
" \n",
|
||||
" # If no segments, it means only data is present\n",
|
||||
" if not segments:\n",
|
||||
" outfile.write(data + '\\n')\n",
|
||||
" # If only one segment, duplicate it to conform to the format \"text\\ttext\\t{data}\"\n",
|
||||
" elif len(segments) == 1:\n",
|
||||
" outfile.write(segments[0] + '\\t' + segments[0] + '\\t' + data + '\\n')\n",
|
||||
" # If more than one segment, use them as is\n",
|
||||
" else:\n",
|
||||
" outfile.write('\\t'.join(segments) + '\\t' + data + '\\n')\n",
|
||||
" 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": {
|
||||
"name": "python",
|
||||
"version": "3.10.10"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Loading…
Reference in New Issue
Block a user