TTYGod
Articles / Workflow

Read a CSV row that has a comma inside a name

A book-club inventory looks perfectly ordinary in a spreadsheet. In a terminal the title Notes, Vol. 2 seems to spill into a third column. The extra comma belongs to the title. A CSV reader can show you exactly which text the spreadsheet stored in each field.

TTYGod editorial desk · AI-assisted launch edition · Issue ·
Share WhatsAppLinkedInEmail
Desk lamp over file cards
Desk lamp over file cards · AI-generated editorial illustration
תקציר בעברית

שם ספר מכיל פסיק, אבל בקובץ CSV הוא עדיין שדה אחד. התרגיל מראה כיצד קורא CSV מפרש מירכאות ואיך לבדוק שהכותרת והמדף סווגו בעמודות הנכונות.

Make a tiny example

In a new practice folder, use a plain text editor to create books.csv. Give the first line the text title,shelf and the second line the text "Notes, Vol. 2",Green. Save the file as UTF-8. The quotation marks around the title are ordinary straight double quotes. They tell a CSV reader that the comma inside the title belongs to a field. The comma after the closing quote separates the title from the shelf. See the Python CSV reader documentation.

Open a local reader

From a terminal in the practice folder run python3 to start an installed Python 3 interpreter. At the interactive prompt enter import csv and press Return. Next enter f = open('books.csv', encoding='utf-8', newline='') and press Return. Now enter rows = list(csv.DictReader(f)), then enter f.close() on its own line. These actions read the sample into memory and close the file. The list is convenient for a tiny practice sheet where you can inspect every row.

Desk lamp over file cards
Desk lamp over file cards · AI-generated editorial illustration

Ask for one column at a time

Enter print(rows[0]['title']) to see Notes, Vol. 2 on a single line. Next enter print(rows[0]['shelf']) to see Green. The first row of the file supplied the names title and shelf. The index zero selects the first data row. A simple split at every comma would have cut the title into pieces and shifted everything after it. The CSV reader respects the surrounding quotes and gives each heading its intended value.

Check the shape of a real export

For a small, freshly exported sheet, inspect its first few lines in your editor before repeating the exercise. Some programs separate fields with tabs or semicolons, and some add introductory lines before the headings. Check how the editor displays the exact spelling of each heading, including spaces and capital letters. If a sheet contains private entries, work in the same local folder and print just the columns you need. Type exit() to leave the interpreter when you finish. For an empty sheet, inspect the header first. Index zero is available only after the reader has found at least one data row. For further reading, browse command line for beginners books.

Before you begin

What should the first data row contain after the title is read?

Check your result

The first title is Notes, Vol. 2 and the shelf is Green. The inner comma remains part of the title.

Reference · See the Python CSV reader documentation

Bookshop.org

For further reading, browse the bookshop catalogue and compare titles and editions.

This merchant link currently earns this publication no commission. Affiliate disclosure

Browse command line for beginners books

Reader notes

Read community notes

No reader notes yet.

Related articles

Mechanical keyboard and paper notes
Issue ·
Workflow

Find the note that mentions a blue envelope

You remember putting the pickup instructions in a text file. The date, the filename and the folder have slipped your mind. What stayed with you is the phrase “blue envelope.” That fragment is enough to make a small, repeatable search.

Notebook beside a terminal monitor
Issue ·
Workflow

Track down a missing draft by its filename

A friend asks for the handout you prepared last month. You remember calling it a draft, and you have a folder full of workshops, but the contents of those folders stretch over several years. Search the names before opening every document.

Laptop on a dark desk
Issue ·
Workflow

Turn a messy packing list into a useful tally

Three people send lists for a picnic and all three volunteer to bring cups. Before assigning anyone another job, put one item on each line and see which promises overlap. This exercise works entirely in a terminal and keeps the original list intact.