業務用csv処理ツール

dataTypes.py
```

class StrByColumn:

    def __init__(self, annotation_string: str = "", column: int = 0) -> None:
        self.annotation_string = annotation_string
        self.column = column

class csvData:
    
    def __init__(self, fileName: str, dataList: list) -> None:
        self.filename = fileName
        self.datalist = dataList

```

readDefTools.py
```
from dataTypes import *

class readDef:

    def __init__(self, defFullPath: str) -> None:
        self.continue_condition = StrByColumn()
        self.is_extract_condition = False
        self.extract_condition = StrByColumn()
        self.extract_column = 0
        
        continue_condition_annotation_string = ""
        extract_condition_annotation_string = ""

        with open(defFullPath, encoding='utf8', newline='') as f:
            for line in f:
                if ("#" in line):
                    pass
                else:
                    arg_atr = line.split("=")[0].strip()
                    arg_val = line.split("=")[1].strip()
                    if (arg_atr == "continue_condition.annotation_string"):
                        continue_condition_annotation_string = arg_val
                    elif (arg_atr == "continue_condition.column"):
                        self.continue_condition = StrByColumn(continue_condition_annotation_string, int(arg_val))
                    elif (arg_atr == "extract_column"):
                        self.extract_column = int(arg_val)
                    elif (arg_atr == "extract_condition"):
                        if (arg_val == "true"):
                            self.is_extract_condition = True
                    elif (arg_atr == "extract_condition.annotation_string"):
                        extract_condition_annotation_string = arg_val
                    elif (arg_atr == "extract_condition.column"):
                        self.extract_condition = StrByColumn(extract_condition_annotation_string, int(arg_val))
```

def.txt
```
continue_condition.annotation_string=測定
continue_condition.column=0
extract_column=3
# true or false
extract_condition=false
extract_condition.annotation_string=1
extract_condition.column=2
```

csvTools.py
```
import csv
import glob
import os
from decimal import Decimal

from dataTypes import *
from readDefTools import *

class csvExtractor:

    def __init__(self, csvFullPath: str, continue_condition: StrByColumn, extract_column: int, extract_condition: StrByColumn = None) -> None:
        self.raw_list = self.csvToList(csvFullPath)
        
        extracted_values = []
        counter: int = 0
        for raw_list_row in self.raw_list:
            if (raw_list_row[continue_condition.column] == continue_condition.annotation_string) & (counter == 0):
                counter += 1
            if (raw_list_row[continue_condition.column] != continue_condition.annotation_string) & (counter == 1):
                break
            if (counter == 1):
                if (extract_condition is None):
                    extracted_values.append(raw_list_row[extract_column])
                elif (type(extract_condition) is StrByColumn):
                    if (raw_list_row[extract_condition.column] == extract_condition.annotation_string):
                        extracted_values.append(raw_list_row[extract_column])
                else:
                    print("csvExtractor: extract_condition type error.")
        filename = csvFullPath.split("\\")[-1].split(".")[0]
        self.csvDataObj = csvData(filename, extracted_values)

    def csvToList(self, csvPath) -> list:
        resultListCsvToList = []
        with open(csvPath, encoding='utf8', newline='') as f:
            csvreader = csv.reader(f)
            for row in csvreader:
                resultListCsvToList.append(row)
        return resultListCsvToList
    
    


# ディレクトリ中のcsvからcsvDataオブジェクトを生成
class csvExtractFromDirectory:

    def __init__(self, directoryFullPath: str, defObj: readDef) -> None:
        self.csvFullPathList = []
        self.csvDatas = []
        self.directory_name = directoryFullPath.split("\\")[-1]

        files = glob.glob(directoryFullPath + "\\*")
        for file in files:
            if (self.isCsv(file)):
                self.csvFullPathList.append(file)
        for csvFullPath in self.csvFullPathList:
            extract_condition = None
            if (defObj.is_extract_condition):
                extract_condition = defObj.extract_condition
            csvExtractorObj = csvExtractor(csvFullPath, defObj.continue_condition, defObj.extract_column, extract_condition)
            self.csvDatas.append(csvExtractorObj.csvDataObj)
        

    def isCsv(self, csvFullPath) -> bool:
        if (csvFullPath.split(".")[-1] == "csv"):
            return True
        else:
            return False
        
    # [値たち...]
    def getValues(self) -> list:
        resultList = []
        for csvDataElement in self.csvDatas:
            resultList += csvDataElement.datalist
        return resultList
    
    # [Decimal(str)された値たち...]
    def getDecimalValues(self) -> list:
        resultList = []
        for value in self.getValues():
            resultList.append(Decimal(value))
        return resultList
    
    def get_sigma(self) -> float:
        values = self.getDecimalValues()
        number_of_values = Decimal(len(values))
        average_of_values = Decimal(sum(values)) / number_of_values
        powerdiffs = []
        for v in values:
            powerdiffs.append(pow((v - average_of_values), 2))
        return sum(powerdiffs) / number_of_values

    def writeCsv(self, csv_path) -> None:
        write_csv = []
        write_csv.append(["sigma", self.get_sigma()])
        write_csv.append([])
        write_csv.append(["csv name", "value"])
        write_csv += self.getFormattedList()
        with open(csv_path + "\\" + self.directory_name + ".csv", 'w', newline='') as file:
            writer = csv.writer(file)
            for row in write_csv:
                writer.writerow(row)
                
    
    # [[csv名, 値], ...]
    def getFormattedList(self) -> list:
        resultList = []
        for csvDataElement in self.csvDatas:
            for csvValue in csvDataElement.datalist:
                resultList.append([csvDataElement.filename, csvValue])
        return resultList

        
#csvフォルダからcsvExtractFromDirectoryオブジェクトを生成
class csvDirectoryData:

    def __init__(self) -> None:
        self.csvExtractFromDirectoryObjs = []
        self.defFullPath = os.getcwd() + "\\def.txt"
        self.defObj = readDef(self.defFullPath)

        files = glob.glob(os.getcwd() + "\\csv\\*")
        for file in files:
            self.csvExtractFromDirectoryObjs.append(csvExtractFromDirectory(file, self.defObj))

    def writeCsvs(self) -> None:
        csv_destination = os.getcwd() + "\\result"
        for d in self.csvExtractFromDirectoryObjs:
            d.writeCsv(csv_destination)


```

main.py
```
from csvTools import *
import os

c = csvDirectoryData()
c.writeCsvs()

print("okay")

os.system('PAUSE')
```