100 Exercises / Python / 100 Python Exercises for Data Analysis
Reading and Writing Manufacturing Reports and CSV Files
Read and write manufacturing reports and CSV files. Check encodings and types, summarize quality and sales, and export tables and charts.
100 Exercises Chapter 6 (No.051-No.060): File operations and CSV
This is Chapter 6 (No.051–060) of 100 Python Exercises for Data Analysis. This chapter covers text and CSV input and output using fictional quality inspection and sales data.
About this material These notebooks were used in corporate training by Surikobo or its representative, Hiroshi Wayama, and were adapted for publication with permission. All data are fictional and do not represent actual company or factory results.
Example task
Mr. S, who is in charge of quality control at an auto parts factory, does this kind of work every day.
daily work
1. Check the quality data (CSV) output from the inspection terminal of each line
2. Record the warning details of lines whose defect rate exceeds the threshold in a text file
3. Aggregate quality inspection CSV at the end of the month and output a summary of defect rate by line.
4. Create a report by summarizing monthly sales by product line from the sales CSV.
Python can automate repeated file loading, aggregation, and export steps, reducing manual copying.
Common situations in the field
| Scene | Task |
|---|---|
| Quality data collection | CSV is automatically output from inspection terminals on each line, but aggregation is done manually |
| Alert record | Manually copying and pasting the alert content when the threshold is exceeded into Excel |
| Monthly aggregation | At the end of the month, 30 days worth of CSV is checked line by line and totaled |
| Exporting results | Manually copying aggregation results to a new CSV or text |
| Sales aggregation | It takes several hours every month to compile the sales CSV on a monthly basis |
If you use Python’s open() / csv module, you can automatically process these regardless of the number of data rows.
Common mistakes
While it may seem like “just open the file and read it,” in the context of manufacturing data, it has the following pitfalls:
-
Forgetting to close files If an error occurs after
f = open(...),f.close()will not be called. The file handle remains. You can completely prevent this problem by using thewithstatement -
Character code issue (UTF-8 vs Shift-JIS) The CSV output by the factory inspection terminal may be Windows Japanese encoding (cp932) It is safe to specify
open(..., encoding='utf-8')orencoding='cp932' -
All CSV values are string types The number read by
csv.readeris a string'850'Must be converted to numeric type withint(row[4])orfloat(row[6]) -
Incorrectly treating header rows as data If you don’t skip the first row (column names) with
next(reader), I get a conversion error likeint('Production count') -
Double line break due to omission of
newline=''If you omitopen(..., newline='')when usingcsv.writer, An issue occurs where a blank line is inserted at the end of a line in a Windows environment.
Exercises in this chapter
| No. | Title | Usage in manufacturing industry |
|---|---|---|
| 051 | Load file | Load production daily report text with open() |
| 052 | Write to file | Export quality alert report to text file |
| 053 | Handle files safely with the with statement | Safely read and write line summary summaries with open() |
| 054 | Understanding the structure of CSV files | Quality inspection Check the raw structure of CSV (delimiters, headers, columns) |
| 055 | Load CSV with csv module | Load quality inspection CSV with csv.reader |
| 056 | Process CSV rows one by one | Determine the defect rate of each row and total the number of alerts |
| 057 | Extract specific columns from CSV | Extract product code, production quantity, and defect rate by column index |
| 058 | Totaling numerical columns in CSV | Aggregating and visualizing production counts and defects by line directly from CSV |
| 059 | Export processing results to CSV | Export aggregation summary to a new CSV file |
| 060 | Aggregating monthly sales from sales CSV | Aggregating and graphing sales CSV by month and product using DictReader |
Preparing the Python environment
Run cells in order from the top. Outputs are examples; the OS, versions, and file paths vary by environment. The setup cells import libraries and configure random data and plotting.
import platform
import sys
print(f"OS: {platform.platform()}")
print(f"Python: {sys.version.split()[0]}")
OS: macOS-26.5.2-arm64-arm-64bit-Mach-O
Python: 3.14.4
In the outputs, <temporary directory> represents the folder created during execution.
Setup: Imports and display settings (expand)
import csv
import os
import tempfile
from collections import defaultdict
from datetime import date, timedelta
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
matplotlib.rcParams['font.family'] = 'Hiragino Maru Gothic Pro'
%config InlineBackend.figure_format = 'svg'
np.random.seed(42)
print("Library loading completed")
print(f" NumPy : {np.__version__}")
print(f" Matplotlib : {matplotlib.__version__}")
Library loading completed
NumPy : 2.5.1
Matplotlib : 3.11.0
Creating the sample data
Assumed scenario: Auto parts factory / quality control/sales management team Data type: Production daily report text/quality inspection CSV (3 months)/sales CSV (3 months)
Save the file in your system’s temporary directory (tempfile.mkdtemp()). In practice, specify the path of a shared folder or a predetermined directory.
Setup: Generate the sample data (expand)
# ===== File save destination =====
WORK_DIR = tempfile.mkdtemp(prefix="kobo_python_06_")
REPORT_TXT = os.path.join(WORK_DIR, "kobo_daily_report.txt")
QUALITY_CSV = os.path.join(WORK_DIR, "kobo_quality_check.csv")
SUMMARY_CSV = os.path.join(WORK_DIR, "kobo_quality_summary.csv")
SALES_CSV = os.path.join(WORK_DIR, "kobo_monthly_sales.csv")
np.random.seed(42)
# ===== Line/Product Master =====
lines_config = [
{"line": "A-line", "code": "EG-1001", "name": "Engine parts A", "plan": 850, "price": 4200, "defect_base": 0.012},
{"line": "B line", "code": "EG-1002", "name": "Engine parts B", "plan": 720, "price": 3800, "defect_base": 0.018},
{
"line": "C line",
"code": "HN-2001",
"name": "harness unit",
"plan": 1200,
"price": 1500,
"defect_base": 0.025,
},
{
"line": "D line",
"code": "SN-3001",
"name": "sensor module",
"plan": 950,
"price": 2800,
"defect_base": 0.015,
},
{"line": "E line", "code": "FR-4001", "name": "frame parts", "plan": 630, "price": 5500, "defect_base": 0.010},
]
# ===== Business days generated from January to March 2025 =====
def business_days(start, end):
days = []
d = start
while d <= end:
if d.weekday() < 5:
days.append(d)
d += timedelta(days=1)
return days
bdays = business_days(date(2025, 1, 6), date(2025, 3, 31))
# ===== Generation of quality inspection CSV (305 rows) =====
quality_rows = []
for d in bdays:
for lc in lines_config:
actual = int(lc["plan"] * np.random.uniform(0.92, 1.08))
dr = lc["defect_base"] * np.random.uniform(0.5, 2.5)
defect = int(actual * dr)
# Calculate the stored rate from the integer defect count.
quality_rows.append(
{
"date": d.isoformat(),
"line": lc["line"],
"Product code": lc["code"],
"Product name": lc["name"],
"Production count": actual,
"Number of defects": defect,
"Defect rate": round(defect / actual * 100, 2),
}
)
with open(QUALITY_CSV, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["date", "line", "Product code", "Product name", "Production count", "Number of defects", "Defect rate"])
writer.writeheader()
writer.writerows(quality_rows)
# ===== Generation of production daily report text =====
last_date = bdays[-1]
last_rows = [r for r in quality_rows if r["date"] == last_date.isoformat()]
report_lines_list = [
f"=== {last_date.isoformat()} production daily report ===",
"Person in charge: Taro Tanaka",
"Approved by: Manager Yamamoto",
"",
"[Production results by line]",
]
for r in last_rows:
report_lines_list.append(
f" {r['line']}: production {r['Production count']:,}units / defective {r['Number of defects']}units / Defect rate {r['Defect rate']:.2f}%"
)
report_lines_list += [
"",
"[Summary]",
"Today's production on all lines generally proceeded as planned.",
"Sample summary: line C is at or above 3.0%, so an inspection is planned for the next day.",
]
with open(REPORT_TXT, "w", encoding="utf-8") as f:
f.write("\n".join(report_lines_list))
# ===== Generate sales CSV =====
sales_rows = []
for d in bdays:
for lc in lines_config:
qty = int(lc["plan"] * np.random.uniform(0.85, 1.15))
amount = qty * lc["price"]
sales_rows.append(
{
"date": d.isoformat(),
"Product code": lc["code"],
"Product name": lc["name"],
"quantity": qty,
"unit price": lc["price"],
"amount": amount,
}
)
with open(SALES_CSV, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["date", "Product code", "Product name", "quantity", "unit price", "amount"])
writer.writeheader()
writer.writerows(sales_rows)
print("=== Fictitious data generation completed ===")
print(f"quality inspection CSV : {QUALITY_CSV}")
print(f" number of lines : {len(quality_rows):,} Row (excluding header)")
print(f"production daily report TXT : {REPORT_TXT}")
print(f"sales CSV : {SALES_CSV}")
print(f" number of lines : {len(sales_rows):,} Row (excluding header)")
print(f"Business days : {len(bdays)} day (2025-01-06~2025-03-31)")
=== Fictitious data generation completed ===
quality inspection CSV : <temporary directory>/kobo_quality_check.csv
number of lines : 305 Row (excluding header)
production daily report TXT : <temporary directory>/kobo_daily_report.txt
sales CSV : <temporary directory>/kobo_monthly_sales.csv
number of lines : 305 Row (excluding header)
Business days : 61 day (2025-01-06~2025-03-31)
No.051: Load file
Use in practice
Production systems can export logs and daily reports as text files. Reading them with open() is the starting point for file processing.
Approach
Open the file with a path, mode, and encoding. read() returns the whole file as a string, readlines() returns a list of lines, and readline() reads one line. Use the method that fits the file size and task.
Check with Python
# No.051: Read the file with open()
print(f"=== No.051: load file ===")
print(f"loading file: {REPORT_TXT}")
print()
# Open a file in read mode ("r") with open()
f = open(REPORT_TXT, "r", encoding="utf-8")
content = f.read() # Read the entire file as one string
f.close() # Always close() (understood as a step before the with statement)
print(content)
print()
print(f"--- File information ---")
print(f"Number of characters: {len(content):,} characters")
print(f"number of lines : {len(content.splitlines())} rows")
=== No.051: load file ===
loading file: <temporary directory>/kobo_daily_report.txt
=== 2025-03-31 production daily report ===
Person in charge: Taro Tanaka
Approved by: Manager Yamamoto
[Production results by line]
A-line: production 804units / defective 10units / Defect rate 1.24%
B line: production 682units / defective 8units / Defect rate 1.17%
C line: production 1,127units / defective 40units / Defect rate 3.55%
D line: production 905units / defective 16units / Defect rate 1.77%
E line: production 630units / defective 11units / Defect rate 1.75%
[Summary]
Today's production on all lines generally proceeded as planned.
Sample summary: line C is at or above 3.0%, so an inspection is planned for the next day.
--- File information ---
Number of characters: 648 characters
number of lines : 14 rows
Interpreting the results
f.read() returns the entire file as one string, including newlines. The report contains results for March 31, 2025; line C has a 3.55% defect rate. The summary paragraph is sample text set in the code, not an automatic judgment of plan achievement or change from the previous day.
Close a file after using open(). No.053 introduces with, which also closes the file when an exception occurs.
No.052: Write to file
Use in practice
Save quality warnings in a text report so the team can review which lines exceeded the threshold.
Approach
Open a path with open(path, "w") and write text with write(). Mode "w" replaces existing contents; mode "a" appends. Use append mode for a cumulative log and write mode for a report rebuilt from its inputs.
Check with Python
# No.052: Write to file with open()
ALERT_TXT = os.path.join(WORK_DIR, "kobo_quality_alert.txt")
ALERT_THRESHOLD = 3.0 # Defect rate alert threshold (%)
# Obtain quality data for the latest date (last business day)
latest_date = max(r["date"] for r in quality_rows)
latest_rows = [r for r in quality_rows if r["date"] == latest_date]
# Open file in write mode ("w")
f = open(ALERT_TXT, "w", encoding="utf-8")
f.write(f"=== Quality alert report ({latest_date}) ===\n")
f.write(f"Judgment threshold: Defect rate {ALERT_THRESHOLD}% That's all\n\n")
alert_count = 0
for r in latest_rows:
dr = float(r["Defect rate"])
if dr >= ALERT_THRESHOLD:
f.write(f"[warning] {r['line']}({r['Product code']}): Defect rate {dr:.2f}%\n")
alert_count += 1
if alert_count == 0:
f.write("All lines are within normal range today\n")
f.write(f"\nNumber of warning lines: {alert_count} items\n")
f.close() # Don't forget close()
print(f"Writing completed: {ALERT_TXT}")
print()
# Read the write result for confirmation
f_read = open(ALERT_TXT, "r", encoding="utf-8")
print(f_read.read())
f_read.close()
Writing completed: <temporary directory>/kobo_quality_alert.txt
=== Quality alert report (2025-03-31) ===
Judgment threshold: Defect rate 3.0% That's all
[warning] C line(HN-2001): Defect rate 3.55%
Number of warning lines: 1 items
Interpreting the results
The code checks the final weekday’s data for rates of at least 3.0% and writes matching lines to a report. Mode "w" replaces the report on each run. Mode "a" would append, so rerunning the same inputs could add duplicate entries.
No.053: Handle files safely with the with statement
Use in practice
A manual close() call may be skipped if an earlier step raises an exception. A with open(...) as f: block closes the file on exit, including when an exception occurs.
Approach
with open(...) as f: uses a context manager to close the file after the block. Iterate with for line in f: when processing a text file one line at a time.
Check with Python
# No.053: Safely read and write files with the with statement
SUMMARY_TXT = os.path.join(WORK_DIR, "kobo_line_summary.txt")
print("=== No.053: Handle files safely with the with statement ===")
print()
# --- Write ---
# f.close() is automatically called when you exit the with block.
with open(SUMMARY_TXT, "w", encoding="utf-8") as f:
f.write("=== Quality summary by line (January to March 2025) ===\n\n")
for lc in lines_config:
rows = [r for r in quality_rows if r["line"] == lc["line"]]
total_p = sum(r["Production count"] for r in rows)
total_d = sum(r["Number of defects"] for r in rows)
avg_dr = total_d / total_p * 100 if total_p else 0.0
f.write(f" {lc['line']} ({lc['code']}): Cumulative production {total_p:,}units Average defect rate {avg_dr:.2f}%\n")
f.write("\n--- Generation completed ---\n")
# ← f.close() is automatically executed here
print(f"Writing completed: {SUMMARY_TXT}")
print()
# --- Read (one line at a time)---
print("=== File contents (read line by line) ===")
with open(SUMMARY_TXT, "r", encoding="utf-8") as f:
for line_text in f: # Read file line by line
print(line_text, end="") # Specify end="" to avoid duplicate line breaks
=== No.053: Handle files safely with the with statement ===
Writing completed: <temporary directory>/kobo_line_summary.txt
=== File contents (read line by line) ===
=== Quality summary by line (January to March 2025) ===
A-line (EG-1001): Cumulative production 51,690units Average defect rate 1.89%
B line (EG-1002): Cumulative production 43,727units Average defect rate 2.71%
C line (HN-2001): Cumulative production 73,157units Average defect rate 3.85%
D line (SN-3001): Cumulative production 57,698units Average defect rate 2.17%
E line (FR-4001): Cumulative production 38,546units Average defect rate 1.29%
--- Generation completed ---
Interpreting the results
The file contains summaries for five lines. The period defect rate, total defective units divided by total production, is highest for line C at 3.85% and lowest for line E at 1.29%. A lower average alone does not establish day-to-day stability.
for line_text in f: reads a text file one line at a time, avoiding the need to load the entire file into one string.
No.054: Understand the structure of CSV files
Use in practice
Check a CSV file’s headers, delimiters, and expected types before processing data from a factory system or ERP.
Approach
This CSV starts with column names followed by data records. CSV files may also omit headers, and quoted fields may contain commas or line breaks.
The example uses split(",") only to inspect simple rows without embedded commas or line breaks. Use csv.reader to parse general CSV files.
Check with Python
# No.054: Check the structure of the CSV file with raw strings
print(f"=== No.054: CSV Understand the structure of files ===")
print(f"file: {QUALITY_CSV}")
print()
with open(QUALITY_CSV, "r", encoding="utf-8") as f:
all_lines = f.readlines()
total_rows = len(all_lines) - 1 # excluding header
print(f"Total number of lines: {len(all_lines)} row (header 1 rows + data {total_rows} line)")
print()
# Display first 5 lines as raw string
print("=== First 5 rows of raw data (comma separated structure) ===")
for i, line in enumerate(all_lines[:5]):
print(f" [{i}] {line.rstrip()}")
print()
# Split header and check column index
header_raw = all_lines[0].rstrip().split(",")
print("=== Check column index ===")
for i, col in enumerate(header_raw):
print(f" columns[{i}]: {col}")
print()
# Decompose and examine a single row of data
sample = all_lines[1].rstrip().split(",")
print("=== Result of decomposing one row of data into columns ===")
for col, val in zip(header_raw, sample):
typ = "numerical value" if val.replace(".", "").replace("-", "").isdigit() else "string"
print(f" {col}: {val!r} ({typ})")
=== No.054: CSV Understand the structure of files ===
file: <temporary directory>/kobo_quality_check.csv
Total number of lines: 306 row (header 1 rows + data 305 line)
=== First 5 rows of raw data (comma separated structure) ===
[0] date,line,Product code,Product name,Production count,Number of defects,Defect rate
[1] 2025-01-06,A-line,EG-1001,Engine parts A,832,23,2.76
[2] 2025-01-06,B line,EG-1002,Engine parts B,746,22,2.95
[3] 2025-01-06,C line,HN-2001,harness unit,1133,22,1.94
[4] 2025-01-06,D line,SN-3001,sensor module,882,29,3.29
=== Check column index ===
columns[0]: date
columns[1]: line
columns[2]: Product code
columns[3]: Product name
columns[4]: Production count
columns[5]: Number of defects
columns[6]: Defect rate
=== Result of decomposing one row of data into columns ===
date: '2025-01-06' (numerical value)
line: 'A-line' (string)
Product code: 'EG-1001' (string)
Product name: 'Engine parts A' (string)
Production count: '832' (numerical value)
Number of defects: '23' (numerical value)
Defect rate: '2.76' (numerical value)
Interpreting the results
This file has one header row and 305 data rows. Column 0 is the date, column 4 is production, and column 6 is the defect rate. Splitting this simple example produces strings; convert numeric fields with int() or float() before calculating.
No.055: Load CSV with csv module
Use in practice
The csv module handles CSV quoting, including quoted fields that contain commas or line breaks. Use it for CSV data rather than manually splitting each line.
Approach
csv.reader(f) yields each CSV record as a list of fields. If the file has a header, read it with next(reader) before processing the remaining records with for row in reader:.
Open the file with newline="" so the CSV module handles line endings. This supports embedded newlines in quoted fields and avoids unwanted newline conversion on Windows.
Check with Python
# No.055: Read CSV with csv.reader
print("=== No.055: Load CSV with csv.reader ===")
print()
rows_055 = []
with open(QUALITY_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
header_055 = next(reader) # Get header row (skip)
for row in reader:
rows_055.append(row) # Store each row as a list
print(f"header : {header_055}")
print(f"Number of data items: {len(rows_055):,} rows")
print()
print("=== First 5 lines (list read with csv.reader) ===")
fmt = f" {{:<12}} {{:<10}} {{:<10}} {{:>8}} {{:>8}} {{:>8}}"
print(fmt.format(header_055[0], header_055[1], header_055[2], header_055[4], header_055[5], header_055[6]))
print(" " + "-" * 62)
for row in rows_055[:5]:
print(fmt.format(row[0], row[1], row[2], row[4], row[5], row[6]))
print()
# Check header row type
print("=== csv.reader row type ===")
print(f" header row type : {type(header_055)}, element type: {type(header_055[0])}")
print(f" data row type : {type(rows_055[0])}, element type: {type(rows_055[0][4])!r} <- Even numbers can be strings!")
=== No.055: Load CSV with csv.reader ===
header : ['date', 'line', 'Product code', 'Product name', 'Production count', 'Number of defects', 'Defect rate']
Number of data items: 305 rows
=== First 5 lines (list read with csv.reader) ===
date line Product code Production count Number of defects Defect rate
--------------------------------------------------------------
2025-01-06 A-line EG-1001 832 23 2.76
2025-01-06 B line EG-1002 746 22 2.95
2025-01-06 C line HN-2001 1133 22 1.94
2025-01-06 D line SN-3001 882 29 3.29
2025-01-06 E line FR-4001 640 12 1.88
=== csv.reader row type ===
header row type : <class 'list'>, element type: <class 'str'>
data row type : <class 'list'>, element type: <class 'str'> <- Even numbers can be strings!
Interpreting the results
csv.reader returns each record as a list of strings. Convert production with int(row[4]) and the defect rate with float(row[6]). The reader handles delimiters and quoting, which simple comma splitting does not.
No.056: Process CSV rows one by one
Use in practice
Read the 305 inspection records and count rows with a defect rate of 3.0% or higher. The same for row in reader: loop can process additional records.
Approach
“Row-by-row processing” applies judgment and aggregation processing to each row of CSV.
- Count number of alerts: conditional
if+ counter variable - Filter data: only rows that meet the conditions are stored in a separate list
- Aggregation: Add numbers for each row (detailed in
No.058)
Check with Python
# No.056: Aggregate defect rate alerts by processing CSV rows one by one
print("=== No.056: Process CSV rows one by one ===")
print()
DEFECT_THRESHOLD_056 = 3.0 # Defect rate alert threshold (%)
alert_rows_056 = []
normal_count_056 = 0
alert_count_056 = 0
with open(QUALITY_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
next(reader) # skip header
for row in reader:
rate_f = float(row[6]) # Column [6] = Defect rate (converted from string to float)
if rate_f >= DEFECT_THRESHOLD_056:
alert_rows_056.append(
{
"date": row[0],
"line": row[1],
"Product code": row[2],
"Defect rate": rate_f,
}
)
alert_count_056 += 1
else:
normal_count_056 += 1
total_056 = alert_count_056 + normal_count_056
print(f"All inspection records : {total_056:,} records")
print(f"normal (< {DEFECT_THRESHOLD_056}%) : {normal_count_056:,} records")
print(f"alert (>= {DEFECT_THRESHOLD_056}%): {alert_count_056:,} records ({alert_count_056/total_056*100:.1f}%)")
print()
# Number of alerts per line
line_alerts = defaultdict(int)
for r in alert_rows_056:
line_alerts[r["line"]] += 1
print(f"=== Number of alerts by line (defect rate >= {DEFECT_THRESHOLD_056}% days)===")
print(f"{'line':<10} {'Number of alerts':>12} {'Overall ratio':>8}")
print("-" * 34)
for line_name in sorted(line_alerts.keys()):
cnt = line_alerts[line_name]
bdays_cnt = total_056 // len(lines_config)
pct = cnt / bdays_cnt * 100
print(f"{line_name:<10} {cnt:>12} records {pct:>7.1f}%")
print()
print(f"=== Defect rate TOP5 ===")
top5 = sorted(alert_rows_056, key=lambda x: x["Defect rate"], reverse=True)[:5]
for i, r in enumerate(top5, 1):
print(f" {i}rank: {r['date']} {r['line']} ({r['Product code']}): {r['Defect rate']:.2f}%")
=== No.056: Process CSV rows one by one ===
All inspection records : 305 records
normal (< 3.0%) : 228 records
alert (>= 3.0%): 77 records (25.2%)
=== Number of alerts by line (defect rate >= 3.0% days)===
line Number of alerts Overall ratio
----------------------------------
B line 23 records 37.7%
C line 42 records 68.9%
D line 12 records 19.7%
=== Defect rate TOP5 ===
1rank: 2025-03-12 C line (HN-2001): 6.14%
2rank: 2025-02-17 C line (HN-2001): 6.11%
3rank: 2025-03-26 C line (HN-2001): 5.99%
4rank: 2025-03-07 C line (HN-2001): 5.95%
5rank: 2025-03-20 C line (HN-2001): 5.92%
Interpreting the results
Processing the 305 records gives 228 normal records and 77 alerts (25.2%). Line C meets or exceeds 3.0% on 42 of 61 days (68.9%), the largest alert count. Its maximum is 6.14% on March 12, 2025. Lines A and E have no days meeting this alert threshold.
float(row[6]) converts the string to a number, and rate_f >= DEFECT_THRESHOLD_056 classifies it. Being below a threshold is different from demonstrating statistical process stability.
No.057: Extract specific columns from CSV
Use in practice
Extract only “product code, production quantity, defect rate” instead of all columns** A typical task in creating quality control reports is to compile the average production quantity and average defect rate for each product. Defining column indexes as constants makes it easier to modify when columns are added or removed.
Approach
Retrieve a specific column with row[index]. Defining the column index as a constant variable (e.g. IDX_PROD = 4)
Even if the column order of CSV changes, it can be handled by making a change in one place. By using defaultdict for aggregation, you can omit the dictionary initialization code.
Check with Python
# No.057: Extract specific columns from CSV and perform aggregation by product code
# Define the column index as a constant (even if the CSV column structure changes, you only need to modify it in one place)
IDX_DATE = 0
IDX_LINE = 1
IDX_CODE = 2
IDX_NAME = 3
IDX_PROD = 4
IDX_DEFECT = 5
IDX_RATE = 6
print("=== No.057: Extract specific columns from CSV ===")
print()
# Extract only 3 columns: product code, production quantity, defect rate
code_stats = defaultdict(lambda: {"Production count": 0, "Total defect rate": 0.0, "Number of cases": 0, "name": ""})
with open(QUALITY_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
code = row[IDX_CODE]
prod = int(row[IDX_PROD]) # Convert column[4] to int
rate = float(row[IDX_RATE]) # Convert column [6] to float
code_stats[code]["Production count"] += prod
code_stats[code]["Total defect rate"] += rate
code_stats[code]["Number of cases"] += 1
code_stats[code]["name"] = row[IDX_NAME]
print("=== Cumulative production quantity/average defect rate by product code ===")
print(f"{'code':<10} {'Product name':<18} {'Cumulative production count':>12} {'Average defect rate':>10}")
print("-" * 56)
for code in sorted(code_stats.keys()):
s = code_stats[code]
avg = s["Total defect rate"] / s["Number of cases"]
print(f"{code:<10} {s['name']:<18} {s['Production count']:>12,} units {avg:>9.2f}%")
=== No.057: Extract specific columns from CSV ===
=== Cumulative production quantity/average defect rate by product code ===
code Product name Cumulative production count Average defect rate
--------------------------------------------------------
EG-1001 Engine parts A 51,690 units 1.90%
EG-1002 Engine parts B 43,727 units 2.71%
FR-4001 frame parts 38,546 units 1.28%
HN-2001 harness unit 73,157 units 3.84%
SN-3001 sensor module 57,698 units 2.17%
Interpreting the results
The output gives total production and the arithmetic mean of daily defect rates for each product. Defining column positions in one place, such as IDX_PROD = 4, makes changes easier when the CSV order changes.
The unweighted mean of daily rates differs from total defects divided by total production when daily production varies. No.058 calculates the latter.
No.058: Sum the numerical columns of CSV
Use in practice
Directly summarizing the cumulative production and defective numbers for each line from CSV is as follows: This process is directly linked to the automatic generation of monthly quality reports. By visualizing the aggregated results in a graph, you can intuitively understand which line is the most risky.
Approach
The basic pattern for summing CSV numerical columns is
Simple cumulative addition of total += int(row[i]).
- Don’t forget type conversion using
int()/float() - Use a dictionary for line-by-line aggregation (
line_totals[line]["Production count"] += prod)
Check with Python
# No.058: Total the CSV numerical columns (production quantity/defective quantity) for each line
print("=== No.058: Sum the numerical columns of CSV ===")
print()
total_prod = 0
total_defect = 0
line_totals = {}
with open(QUALITY_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
prod = int(row[IDX_PROD])
defect = int(row[IDX_DEFECT])
line = row[IDX_LINE]
total_prod += prod
total_defect += defect
if line not in line_totals:
line_totals[line] = {"Production count": 0, "Number of defects": 0, "code": row[IDX_CODE], "name": row[IDX_NAME]}
line_totals[line]["Production count"] += prod
line_totals[line]["Number of defects"] += defect
overall_rate = total_defect / total_prod * 100
print("=== Cumulative production by line (January to March 2025) ===")
print(f"{'line':<10} {'Total production quantity':>12} {'Total number of defects':>10} {'Defect rate':>8}")
print("-" * 46)
for line in sorted(line_totals.keys()):
t = line_totals[line]
rate = t["Number of defects"] / t["Production count"] * 100
print(f"{line:<10} {t['Production count']:>12,} units {t['Number of defects']:>8} units {rate:>7.2f}%")
print("-" * 46)
print(f"{'Total of all lines':<10} {total_prod:>12,} units {total_defect:>8} units {overall_rate:>7.2f}%")
=== No.058: Sum the numerical columns of CSV ===
=== Cumulative production by line (January to March 2025) ===
line Total production quantity Total number of defects Defect rate
----------------------------------------------
A-line 51,690 units 978 units 1.89%
B line 43,727 units 1185 units 2.71%
C line 73,157 units 2817 units 3.85%
D line 57,698 units 1253 units 2.17%
E line 38,546 units 497 units 1.29%
----------------------------------------------
Total of all lines 264,818 units 6730 units 2.54%
# No.058: Visualization of production quantity and defect rate by line
sorted_lines = sorted(line_totals.keys())
prods = [line_totals[l]["Production count"] for l in sorted_lines]
defects = [line_totals[l]["Number of defects"] for l in sorted_lines]
rates = [line_totals[l]["Number of defects"] / line_totals[l]["Production count"] * 100 for l in sorted_lines]
fig, axes = plt.subplots(1, 2, figsize=(13, 5))
x_pos = list(range(len(sorted_lines)))
# Left: Production quantity (accumulation: number of good items + number of defective items)
good_counts = [p - d for p, d in zip(prods, defects)]
axes[0].bar(x_pos, [g / 1000 for g in good_counts], color="#2E86AB", alpha=0.85, label="Number of good products")
axes[0].bar(
x_pos,
[d / 1000 for d in defects],
color="#E74C3C",
alpha=0.85,
label="Number of defects",
bottom=[g / 1000 for g in good_counts],
)
for i, p in enumerate(prods):
axes[0].text(i, p / 1000 + 0.5, f"{p/1000:.1f}k", ha="center", va="bottom", fontsize=9)
axes[0].set_title("Cumulative production volume for 3 months by line (breakdown of good/defective products)", fontsize=13, pad=10)
axes[0].set_xlabel("line", fontsize=11)
axes[0].set_ylabel("Production quantity (thousands)", fontsize=11)
axes[0].set_xticks(x_pos)
axes[0].set_xticklabels(sorted_lines, fontsize=10)
axes[0].legend(fontsize=10)
axes[0].grid(axis="y", alpha=0.3)
# Right: Defect rate (bar graph + threshold line)
bar_colors = ["#E74C3C" if r >= 3.0 else "#2E86AB" for r in rates]
axes[1].bar(x_pos, rates, color=bar_colors, alpha=0.85)
axes[1].axhline(y=3.0, color="red", linewidth=2.0, linestyle="--", label="Warning line (3.0%)")
axes[1].axhline(y=overall_rate, color="gray", linewidth=1.5, linestyle=":", label=f"overall average ({overall_rate:.2f}%)")
for i, r in enumerate(rates):
axes[1].text(i, r + 0.05, f"{r:.2f}%", ha="center", va="bottom", fontsize=9)
axes[1].set_title("3-month average defect rate by line", fontsize=13, pad=10)
axes[1].set_xlabel("line", fontsize=11)
axes[1].set_ylabel("Defect rate (%)", fontsize=11)
axes[1].set_xticks(x_pos)
axes[1].set_xticklabels(sorted_lines, fontsize=10)
axes[1].legend(fontsize=9)
axes[1].grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.show()
Interpreting the results
The 305 records, 61 days times five lines, contain 264,818 produced units with an overall defect rate of 2.54%. Line C’s period rate is 3.85%, above the 3.0% warning threshold. Line E is lowest at 1.29%.
The stacked bars show good and defective units. Line C also has the highest total production, 73,157 units. These measures describe period totals and rates; assess day-to-day stability separately.
No.059: Export processing results to CSV
Use in practice
By exporting the aggregation results to a new CSV file, It is possible to link with other tools (Excel, BI system, ERP). “Posting of summary tables”, which used to be done manually, can be completely automated using code.
Approach
Generate a writer object with csv.writer(f),
writer.writerow([value1, value2, ...])— write one linewriter.writerows([[...], [...], ...])— Write multiple lines at once
The basic pattern is to start the header line with writerow followed by the data line. Specifying open(..., newline='') prevents blank line insertion in Windows environments.
Check with Python
# No.059: Export aggregation results to CSV file
print("=== No.059: Export processing results to CSV ===")
print()
with open(SUMMARY_CSV, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
# header row
writer.writerow(["line", "Product code", "Product name", "Total production quantity", "Total number of defects", "Defect rate", "Total number of non-defective products"])
# Data rows (by line)
for line in sorted(line_totals.keys()):
t = line_totals[line]
rate = round(t["Number of defects"] / t["Production count"] * 100, 2)
good = t["Production count"] - t["Number of defects"]
writer.writerow([line, t["code"], t["name"], t["Production count"], t["Number of defects"], rate, good])
# total line
writer.writerow(
["Total of all lines", "", "", total_prod, total_defect, round(overall_rate, 2), total_prod - total_defect]
)
print(f"Writing completed: {SUMMARY_CSV}")
print()
# Load and check the exported CSV
print("=== Contents of exported CSV (check with csv.reader) ===")
with open(SUMMARY_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
prefix = "header" if i == 0 else f"rows [{i:>2}]"
print(f" {prefix}: {row}")
=== No.059: Export processing results to CSV ===
Writing completed: <temporary directory>/kobo_quality_summary.csv
=== Contents of exported CSV (check with csv.reader) ===
header: ['line', 'Product code', 'Product name', 'Total production quantity', 'Total number of defects', 'Defect rate', 'Total number of non-defective products']
rows [ 1]: ['A-line', 'EG-1001', 'Engine parts A', '51690', '978', '1.89', '50712']
rows [ 2]: ['B line', 'EG-1002', 'Engine parts B', '43727', '1185', '2.71', '42542']
rows [ 3]: ['C line', 'HN-2001', 'harness unit', '73157', '2817', '3.85', '70340']
rows [ 4]: ['D line', 'SN-3001', 'sensor module', '57698', '1253', '2.17', '56445']
rows [ 5]: ['E line', 'FR-4001', 'frame parts', '38546', '497', '1.29', '38049']
rows [ 6]: ['Total of all lines', '', '', '264818', '6730', '2.54', '258088']
Interpreting the results
csv.writer exports seven rows: a header, five production lines, and a total. Reading the file back with csv.reader checks its structure. Pass the column values in order to writer.writerow(); the resulting CSV can also be reviewed in Excel.
No.060: Aggregate monthly sales from sales CSV
Use in practice
To aggregate sales by month and product and create a graph from the sales CSV,
This is the core work of monthly management reports and product line management. csv.DictReader allows access by column name, so
You can write easy-to-read code without worrying about column indexes.
Approach
csv.DictReader(f) returns each row as a dictionary of {Column name: value}. It is highly readable because it can be accessed directly by column name, such as row["amount"].
It is not affected by changing the CSV column order. For monthly aggregation, row["date"][:7] extracts the month key in "2025-01" format and aggregates it in a dictionary.
Check with Python
# No.060: Aggregate sales by month and product from sales CSV (csv.DictReader)
print("=== No.060: Aggregate monthly sales from sales CSV ===")
print()
monthly_amounts = defaultdict(int) # {month key: total amount}
monthly_qty = defaultdict(int) # {month key: total quantity}
code_monthly_060 = defaultdict(lambda: defaultdict(int)) # {Product code: {Month key: Total amount}}
with open(SALES_CSV, "r", encoding="utf-8", newline="") as f:
reader = csv.DictReader(f) # directly accessible by column name
for row in reader:
month_key = row["date"][:7] # "2025-01-06" -> "2025-01"
amount = int(row["amount"])
qty = int(row["quantity"])
code = row["Product code"]
monthly_amounts[month_key] += amount
monthly_qty[month_key] += qty
code_monthly_060[code][month_key] += amount
months_060 = sorted(monthly_amounts.keys())
print("=== Monthly sales summary ===")
print(f"{'month':<10} {'Sales amount':>16} {'Total quantity':>10}")
print("-" * 40)
for m in months_060:
print(f"{m:<10} {monthly_amounts[m]:>16,} JPY {monthly_qty[m]:>8,} units")
print("-" * 40)
grand_total_060 = sum(monthly_amounts.values())
print(f"{'3 months total':<10} {grand_total_060:>16,} JPY")
print()
# Monthly sales by product code
product_codes_060 = sorted(code_monthly_060.keys())
pname_map = {lc["code"]: lc["name"] for lc in lines_config}
print("=== Monthly sales by product code ===")
header_row = f"{'Product code':<10} {'Product name':<18}" + "".join(f" {m:>14}" for m in months_060) + "total"
print(header_row)
print("-" * (len(header_row) + 4))
for code in product_codes_060:
row_vals = [code_monthly_060[code].get(m, 0) for m in months_060]
row_str = f"{code:<10} {pname_map.get(code,''):<18}"
for v in row_vals:
row_str += f" {v:>12,}JPY"
row_str += f" {sum(row_vals):>12,}JPY"
print(row_str)
=== No.060: Aggregate monthly sales from sales CSV ===
=== Monthly sales summary ===
month Sales amount Total quantity
----------------------------------------
2025-01 279,739,400 JPY 85,438 units
2025-02 287,619,600 JPY 88,148 units
2025-03 293,686,100 JPY 89,661 units
----------------------------------------
3 months total 861,045,100 JPY
=== Monthly sales by product code ===
Product code Product name 2025-01 2025-02 2025-03total
-------------------------------------------------------------------------------------
EG-1001 Engine parts A 70,606,200JPY 73,323,600JPY 73,806,600JPY 217,736,400JPY
EG-1002 Engine parts B 53,751,000JPY 55,567,400JPY 56,886,000JPY 166,204,400JPY
FR-4001 frame parts 67,919,500JPY 68,442,000JPY 71,230,500JPY 207,592,000JPY
HN-2001 harness unit 35,203,500JPY 36,759,000JPY 36,813,000JPY 108,775,500JPY
SN-3001 sensor module 52,259,200JPY 53,527,600JPY 54,950,000JPY 160,736,800JPY
# No.060: Monthly sales graph
colors_060 = ["#2E86AB", "#A23B72", "#F18F01", "#C73E1D", "#44BBA4"]
fig, axes = plt.subplots(1, 2, figsize=(13, 5))
x_pos_060 = list(range(len(months_060)))
# Left: Monthly total sales (bar graph)
month_amounts_million = [monthly_amounts[m] / 1_000_000 for m in months_060]
bars_060 = axes[0].bar(x_pos_060, month_amounts_million, color=["#2E86AB", "#A23B72", "#F18F01"], alpha=0.82, width=0.5)
for bar, val in zip(bars_060, month_amounts_million):
axes[0].text(
bar.get_x() + bar.get_width() / 2,
bar.get_height() + 0.3,
f"{val:,.1f}M",
ha="center",
va="bottom",
fontsize=10,
fontweight="bold",
)
axes[0].set_title("Monthly total sales", fontsize=13, pad=10)
axes[0].set_xlabel("Month", fontsize=11)
axes[0].set_ylabel("Sales amount (million yen)", fontsize=11)
axes[0].set_xticks(x_pos_060)
axes[0].set_xticklabels(months_060, fontsize=10)
axes[0].yaxis.set_major_formatter(ticker.FuncFormatter(lambda v, _: f"{v:,.0f}"))
axes[0].grid(axis="y", alpha=0.3)
# Right: Monthly sales by product (stacked bar graph)
bottom_060 = [0.0] * len(months_060)
for i, code in enumerate(product_codes_060):
vals = [code_monthly_060[code].get(m, 0) / 1_000_000 for m in months_060]
axes[1].bar(x_pos_060, vals, bottom=bottom_060, color=colors_060[i], alpha=0.85, label=pname_map.get(code, code))
bottom_060 = [b + v for b, v in zip(bottom_060, vals)]
axes[1].set_title("Monthly sales by product (stacked)", fontsize=13, pad=10)
axes[1].set_xlabel("Month", fontsize=11)
axes[1].set_ylabel("Sales amount (million yen)", fontsize=11)
axes[1].set_xticks(x_pos_060)
axes[1].set_xticklabels(months_060, fontsize=10)
axes[1].yaxis.set_major_formatter(ticker.FuncFormatter(lambda v, _: f"{v:,.0f}"))
axes[1].legend(fontsize=9, loc="upper left")
axes[1].grid(axis="y", alpha=0.3)
plt.tight_layout()
plt.show()
Interpreting the results
Monthly sales are 279,739,400 yen in January, 287,619,600 yen in February, and 293,686,100 yen in March, totaling 861,045,100 yen. Engine part A (EG-1001) is the largest contributor at 217,736,400 yen. The stacked chart shows each product’s contribution. DictReader provides column-name access, and row["date"][:7] creates a month key from the consistently formatted date.
Applying the exercises at work
The file operations and CSV processing learned in No.051-060 are the input/output foundation of manufacturing data analysis.
1. Always use with statement
with open(...) as f: is not just about style, it’s about security. This prevents file locking if you forget to write f.close() and handle leakage in the event of an exception. Do not use the with statement in production data processing. Make it a habit not to write the open() statement.
2. Be aware that CSV values are of string type
All values returned by csv.reader are strings. If you forget to convert int(row[4]) or float(row[6]),
A calculation error '850' + '720' = '850720' (string concatenation) occurs. To deal with conversion errors, combine the patterns learned in Chapter 5 (No.049 try-except).
3. Make column index constant
With the constant definition IDX_PROD = 4, even if the column structure of CSV changes
Only one place needs to be corrected. Furthermore, if you use csv.DictReader, you can access by column name, increasing maintainability.
4. Pass the aggregation results to the next process with csv.writer
By exporting the results aggregated with Python to CSV, Can be linked with Excel, BI tools, and ERP. The flow is “Tally with Python → Export with CSV → Check with Excel” This is the first step in promoting DX at manufacturing sites.
Applying the code to your data
Step 1: Confirm the format of existing CSV (about half a day)
CSV output from on-site inspection terminals and ERP Check “character code, column structure, delimiter, date format”.
| Items to check | Common problems | Solutions |
|---|---|---|
| Character code | Output in Shift-JIS | Specify encoding='cp932' |
| Delimiter | For tab delimited (TSV) | csv.reader(f, delimiter='\t') |
| Date format | 20250106 (8-digit integer) format | Convert with datetime.strptime(d, '%Y%m%d') |
| Comma in number | Number in format '1,200' | int(row[4].replace(',', '')) |
Step 2: Read → Aggregation → Build the export pipeline (about 1 day)
Load CSV with open()
→ Process line by line with csv.reader / DictReader
→ Conditional branching/aggregation (Chapter 4/Chapter 5 skills)
→ Export aggregation results using csv.writer
Step 3: Automate regular execution (about half a day)
With cron (Linux/macOS) or task scheduler (Windows)
The script runs regularly and CSV aggregation is automated every day. When combined with the datetime module in Chapter 5 (No.046-050)
You can implement the process of “loading CSV from a folder with today’s date”.
Step 4: Decision to move to Chapter 8 (Polars)
If the CSV contains hundreds of thousands of rows or more, or if complex aggregation is required,
Consider migrating Polars to read_csv() / group_by(), which you will learn about in Chapter 8. The csv module in this chapter is a Python standard library that has no dependencies and is suitable for small scale.
Polars is used for high-speed and large-scale data.
Summary
We will summarize what we learned in this chapter (No.051-060).
| No. | Skills | Utilization at manufacturing sites |
|---|---|---|
| 051 | Load file | Load production daily report text with open() |
| 052 | Write to file | Export quality alert report to text file |
| 053 | Handle files safely with the with statement | Read and write line summary summaries safely with with open() |
| 054 | Understanding the structure of CSV files | Quality inspection Check the column structure and data type of CSV with raw strings |
| 055 | Load CSV with csv module | Read header data properly with csv.reader |
| 056 | Process CSV rows one by one | Summarize the number of defect rate alerts by scanning all rows |
| 057 | Extract specific columns from CSV | Extract product code and production count using column index constant |
| 058 | Totaling CSV numerical columns | Aggregating and visualizing cumulative production and defective numbers by line |
| 059 | Export processing results to CSV | Export summary summary using csv.writer |
| 060 | Aggregating monthly sales from sales CSV | Aggregating and graphing sales by month and product using DictReader |
Chapter 7 uses NumPy to perform the CSV aggregation process in this chapter.
Learn how to speed up array operations. Functions such as np.sum() / np.mean() are better than for loops.
Aggregation of large amounts of data can be written quickly and concisely.
Training and analysis support
Surikobo provides Python training and data analysis support for manufacturing industry and DX promotion staff.
Do you have any of these problems?
- “I want to automatically aggregate CSV output from inspection terminals/ERP using Python”
- “I want to automatically generate quality reports that are manually copied and pasted every day.”
- “I want to link the results compiled using Python with Excel/BI system”
- “I would like you to create a customized Python training program for your company.”
Services provided
| Service | Overview |
|---|---|
| Python training for the manufacturing industry | Practical training using field data (online/face-to-face) |
| CSV automatic aggregation system construction | Automatic aggregation and report generation of inspection CSV and sales CSV |
| Quality control automation support | Defect rate alert/quality report automation |
| DX promotion consulting | Consistent support from problem organization to implementation |
📩 Contact: www.surikobo.co.jp/contact Please feel free to contact us first.