diff --git a/create_csv_Linesearch_by_extension.ipynb b/create_csv_Linesearch_by_extension.ipynb new file mode 100644 index 0000000..1e53bda --- /dev/null +++ b/create_csv_Linesearch_by_extension.ipynb @@ -0,0 +1,112 @@ +{ + "metadata": { + "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 + }, + "orig_nbformat": 2 + }, + "nbformat": 4, + "nbformat_minor": 2, + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# create_csv_Linesearch_by_extension\n", + "\n", + "**What it does**: \n", + "- searches for numbers within all files in directory with specified extension `findending` e.g. within an output file of a simulation\n", + "\n", + "1. Search for lines (Parameter: `findtext`)\n", + "2. Within the line the number will be found between `findvalue_start` and `findvalue_end`\n", + "3. The data will be saved to `data.csv` and the labels can be renamed from the filename between `filename_to_label_start` and `filename_to_label_end`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# import libs\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import pandas as pd\n", + "import re # for finding text in files\n", + "import glob # for loop over all extensions\n", + "\n", + "\n", + "# variables\n", + "\n", + "## find all fines with *.\n", + "findending = \".out\"\n", + "\n", + "## text so search for\n", + "findtext = [\"stress =\",\n", + "\"displacement =\"]\n", + "findvalue_end = [\"MPa\",\n", + "\"mm\"]\n", + "findvalue_start = \"=\" # start marker for number recognition\n", + "filename_to_label_start = \"\" # start marker for label from filename \n", + "filename_to_label_end = \".out\" # end marker for label from filename \n", + "\n", + "\n", + "# get list of filenames\n", + "filenames = []\n", + "labels = []\n", + "\n", + "for filename in glob.iglob(r\"*\"+findending):\n", + " filenames.extend([filename])\n", + " # cut string\n", + " start = filename.find(filename_to_label_start) + len(filename_to_label_start)\n", + " end = filename.find(filename_to_label_end)\n", + " substring = filename[start:end]\n", + " labels.extend([substring])\n", + "\n", + "# number of filenames\n", + "number_batches=len(filenames)\n", + "\n", + "# dataframe + colums\n", + "cols = []\n", + "for i in range(len(findtext)): \n", + " col = findtext[i]+\" [\"+findvalue_end[i]+\"]\" # columns with units\n", + " cols.extend([col])\n", + "\n", + "# dataframe\n", + "df = pd.DataFrame(columns = cols, index=labels) \n", + "\n", + "# loop for all filenames\n", + "for i in range(number_batches):\n", + "\n", + " # read file\n", + " textfile = open(filenames[i], 'r')\n", + " filetext = textfile.read()\n", + " \n", + " # read value\n", + " for j in range(len(findtext)):\n", + " # get string line\n", + " findtextend = \".*$\"\n", + " fullstring = re.findall(findtext[j]+ findtextend, filetext,re.MULTILINE)[0]\n", + "\n", + " # cut string\n", + " start = fullstring.find(findvalue_start) + len(findvalue_start)\n", + " end = fullstring.find(findvalue_end[j])\n", + " substring = fullstring[start:end]\n", + " df.loc[labels[i], cols[j]] = float(substring)\n", + "\n", + "\n", + "#save\n", + "df.to_csv('data.csv', index=True)" + ] + } + ] +} \ No newline at end of file diff --git a/data.csv b/data.csv new file mode 100644 index 0000000..a5d3451 --- /dev/null +++ b/data.csv @@ -0,0 +1,3 @@ +,stress = [MPa],displacement = [mm] +test1,210.23,0.231 +text2,140.64,0.642 diff --git a/test1.out b/test1.out new file mode 100644 index 0000000..c318ab3 --- /dev/null +++ b/test1.out @@ -0,0 +1,21 @@ +#example file 1 + +--------------------------------------------- +| Simulation Output | +--------------------------------------------- + + +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor +invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et +justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum +dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam +et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. + +stress = 210.23 MPa +displacement = 0.231 mm + +Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum +dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam +et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \ No newline at end of file diff --git a/text2.out b/text2.out new file mode 100644 index 0000000..1a34d8e --- /dev/null +++ b/text2.out @@ -0,0 +1,21 @@ +#example file 2 + +--------------------------------------------- +| Simulation Output | +--------------------------------------------- + + +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor +invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et +justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum +dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam +et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. + +stress = 140.64 MPa +displacement = 0.642 mm + +Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum +dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod +tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam +et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \ No newline at end of file