CSV：指定した列の数値を合計する（Python）

```py
import csv

def add_calculated_score(input_csv: str, output_csv: str, columns_to_sum: list):
    with open(input_csv, newline='', encoding='utf-8') as infile:
        reader = csv.DictReader(infile)
        fieldnames = reader.fieldnames + ['calculatedScore']
        
        rows = []
        for row in reader:
            try:
                sum_value = sum(float(row[col]) for col in columns_to_sum if row[col] != '-')
                row['calculatedScore'] = f"{sum_value:.10g}"  # 小数点第10位まで
            except ValueError:
                row['calculatedScore'] = '-'
            rows.append(row)
        
    with open(output_csv, mode='w', newline='', encoding='utf-8') as outfile:
        writer = csv.DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)

# 使用例
input_csv = 'input.csv'  # 入力CSVファイル名
output_csv = 'output.csv'  # 出力CSVファイル名
columns_to_sum = ['scoreB', 'scoreC']  # 加算する列を指定

add_calculated_score(input_csv, output_csv, columns_to_sum)
```
```py
import csv

def add_calculated_score(input_csv: str, output_csv: str, columns_to_sum: list, insert_after: str):
    with open(input_csv, newline='', encoding='utf-8') as infile:
        reader = csv.DictReader(infile)
        fieldnames = reader.fieldnames
        
        # 指定した列の次に挿入
        if insert_after in fieldnames:
            insert_position = fieldnames.index(insert_after) + 1
        else:
            insert_position = len(fieldnames)  # 指定列がない場合は末尾に追加
        
        rows = []
        for row in reader:
            try:
                sum_value = sum(float(row[col]) for col in columns_to_sum if row[col] != '-')
                row['calculatedScore'] = f"{sum_value:.10g}"  # 小数点第10位まで
            except ValueError:
                row['calculatedScore'] = '-'
            rows.append(row)
        
        fieldnames.insert(insert_position, 'calculatedScore')
    
    with open(output_csv, mode='w', newline='', encoding='utf-8') as outfile:
        writer = csv.DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)

input_csv = 'input.csv'  # 入力CSVファイル名
output_csv = 'output.csv'  # 出力CSVファイル名
columns_to_sum = ['scoreB', 'scoreC']  # 加算する列を指定
insert_after = 'scoreA'  # 計算結果を追加する列の位置を指定

add_calculated_score(input_csv, output_csv, columns_to_sum, insert_after)
```

```py
import csv

def add_calculated_score(input_csv: str, output_csv: str, columns_to_sum: dict, insert_after: str):
    with open(input_csv, newline='', encoding='utf-8') as infile:
        reader = csv.DictReader(infile)
        fieldnames = reader.fieldnames
        
        # 指定した列の次に挿入
        if insert_after in fieldnames:
            insert_position = fieldnames.index(insert_after) + 1
        else:
            insert_position = len(fieldnames)  # 指定列がない場合は末尾に追加
        
        rows = []
        for row in reader:
            try:
                sum_value = sum(float(row[col]) * weight for col, weight in columns_to_sum.items() if row[col] != '-')
                row['calculatedScore'] = f"{sum_value:.10g}"  # 小数点第10位まで
            except ValueError:
                row['calculatedScore'] = '-'
            rows.append(row)
        
        fieldnames.insert(insert_position, 'calculatedScore')
    
    with open(output_csv, mode='w', newline='', encoding='utf-8') as outfile:
        writer = csv.DictWriter(outfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


input_csv = 'input.csv'  # 入力CSVファイル名
output_csv = 'output.csv'  # 出力CSVファイル名
columns_to_sum = {'scoreB': 1.5, 'scoreC': 2.0}  # 各列ごとに指定した数値を掛け合わせる
insert_after = 'scoreA'  # 計算結果を追加する列の位置を指定

add_calculated_score(input_csv, output_csv, columns_to_sum, insert_after)
```