<jupyter_start><jupyter_text>create_csv_Linesearch_by_extension**What it does**: - searches for numbers within all files in directory with specified extension `findending` e.g. within an output file of a simulation1. Search for lines (Parameter: `findtext`)2. Within the line the number will be found between `findvalue_start` and `findvalue_end`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`<jupyter_code># import libs
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import re  # for finding text in files
import glob # for loop over all extensions


# variables

## find all fines with *.
findending = ".out"

## text so search for
findtext = ["stress =",
"displacement ="]
findvalue_end = ["MPa",
"mm"]
findvalue_start = "="    # start marker for number recognition
filename_to_label_start = "" # start marker for label from filename 
filename_to_label_end = ".out" # end marker for label from filename 


# get list of filenames
filenames = []
labels = []

for filename in glob.iglob(r"*"+findending):
    filenames.extend([filename])
    # cut string
    start = filename.find(filename_to_label_start) + len(filename_to_label_start)
    end = filename.find(filename_to_label_end)
    substring = filename[start:end]
    labels.extend([substring])

# number of filenames
number_batches=len(filenames)

# dataframe + colums
cols = []
for i in range(len(findtext)): 
    col = findtext[i]+" ["+findvalue_end[i]+"]" # columns with units
    cols.extend([col])

# dataframe
df = pd.DataFrame(columns = cols, index=labels) 

# loop for all filenames
for i in range(number_batches):

    # read file
    textfile = open(filenames[i], 'r')
    filetext = textfile.read()
    
    # read value
    for j in range(len(findtext)):
        # get string line
        findtextend = ".*$"
        fullstring = re.findall(findtext[j]+ findtextend, filetext,re.MULTILINE)[0]

        # cut string
        start = fullstring.find(findvalue_start) + len(findvalue_start)
        end = fullstring.find(findvalue_end[j])
        substring = fullstring[start:end]
        df.loc[labels[i], cols[j]] = float(substring)


#save
df.to_csv('data.csv', index=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10835509138381201, 'fraction_numerical': 0.0017406440382941688, 'mean_word_length': 4.225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '131183', 'license': 'MIT', 'n_tokens_mistral': 742, 'n_tokens_neox': 723, 'n_words': 214, 'path': 'create_csv_Linesearch_by_extension.ipynb', 'repo_name': 'Fkaule/Plots', 'size': '2298'}