Kids Coding

Files and CSV

Read rows from a CSV-style file. Total a column. Print a clean summary.

KidsAdvancedFiles and CSV

Rows of text

CSV is a common file shape: commas between fields, one row per line. You can write it as text, then split.

Write and total

text = "Sam,12\nRiley,8\nJun,15\n"
with open("scores.csv", "w", encoding="utf-8") as file:
    file.write(text)

total = 0
with open("scores.csv", "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        if not line:
            continue
        name, score = line.split(",")
        total = total + int(score)
        print(name, score)

print("Total:", total)

Every value read from a file arrives as text. Turn "42" into a number with int(...) before you do maths, or "42" + "1" becomes "421" instead of 43.

strip and split

  • strip() removes the newline.
  • split(",") turns a line into a list.
  • int(...) turns the score into a number you can add.

Real life

An attendance file the teacher exports: name, comma, present or absent.

Split on the comma. Count who showed up. That is a CSV in real school software.

Attendance

lines = ["Sam,yes", "Riley,no", "Jun,yes"]
present = 0
for line in lines:
    name, here = line.split(",")
    if here == "yes":
        present = present + 1
        print(name, "is here")
print("Present:", present)

Change one word. Run it. That is how this idea shows up outside the lesson.

Try it

Exercise 1: Split "Sam,12" on a comma and print the name.

Show solution
name, score = "Sam,12".split(",")
print(name)

FAQ: Files and CSV

Common questions about this page.

What is StudyGrid Kids?

StudyGrid Kids is a free coding path for children: Basic, Intermediate, and Advanced. Lessons use Python in the browser. No account and no install.

Should I run files and csv examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn files and csv in this Kids Coding Python lesson (Files and CSV).

Which kids level should we start with?

Start at Basic if the child has never written code. Choose Intermediate if they can print, use variables, and write a simple if. Choose Advanced if they already use loops and lists.

Do kids need an account or an app?

No. Kids pages are public. Code runs in the Try Python editor at /try. We do not ask children for a name or an email.

What language do kids learn?

Python, with a first HTML page in Intermediate and Advanced. After Advanced, the main Python tutorial is the next step.

Is the kids coding track free?

Yes. The Kids home, the three levels, and the Python editor on StudyGrid (studygrid.in) are free.