These free B.Pharm notes on reading and writing csv files in python cover chapter 13 of Basics of Python Programming for Pharmaceutical Sciences (BP101T), a first-semester subject. Read the key points below, then study the complete chapter PDF. You can browse every chapter in the Basics of Python Programming for Pharmaceutical Sciences category.
Table of Contents
Read the following document for full information
Overview of reading and writing csv files in python
These notes explain what a CSV file is, how its header and data rows are structured, and its main characteristics. They then cover file handling modes, reading CSV files with csv.reader() and csv.DictReader(), writing them with csv.writer() and csv.DictWriter(), the advantages and limitations of CSV, and a worked patient dosage example.
Key points on reading and writing csv files in python
- A CSV (comma-separated values) file is a plain-text format storing tabular data, where each row is a record and values are separated by commas.
- The first row of a CSV file usually holds the column headers, and every following row holds the actual data values.
- File mode r opens a file for reading, w overwrites existing content, a appends new data, and r+ allows reading and writing.
- csv.reader() reads each row as a list, while csv.DictReader() reads each row as a dictionary whose keys come from the header row.
- Every value read from a CSV file is a string by default, so numbers must be converted explicitly, for example with int().
Relevance to pharmacy practice
CSV files are widely used to move data such as patient names, drugs and doses between software systems and spreadsheet programs like Microsoft Excel. Reading and writing them in Python helps a pharmacy student process patient records and perform simple dose calculations automatically.
Every chapter in this subject builds on the one before it, so do not skip topics that look simple. Definitions, principles, procedures and examples are the parts that examiners return to most often, and a clear understanding here will also make later subjects easier to follow in the coming semesters.
Frequently asked questions on reading and writing csv files in python
What is the difference between csv.reader() and csv.DictReader()?
csv.reader() returns each row of the file as a plain list of values, while csv.DictReader() returns each row as a dictionary with keys taken from the header row.
Why is newline=”” used when writing a CSV file?
The newline=”” option helps prevent unwanted blank lines from appearing between records in the CSV file, particularly on some systems.
What are the limitations of CSV files?
CSV files do not preserve specific data types, cannot show relationships between data like relational databases, offer limited security and suit large or complex datasets less well.
Quick revision checklist
Use this self-test after reading the chapter on reading and writing csv files in python. If you can do all of the following without looking at your notes, you are ready for the exam.
- I can explain: the structure of a CSV file
- I can explain: characteristics of CSV files
- I can explain: the four common file handling modes
- I can explain: reading CSV files with csv.DictReader()
- I can explain: writing CSV files with csv.writer()
Study and exam tips
Start by reading the chapter once for the overall idea, then revise reading and writing csv files in python using headings, definitions, principles, reactions and examples. Short-answer questions in the university exam usually ask for definitions, principles, procedures, reactions and uses, so write these in your own words and practise the equations, tables and flow charts that the chapter has.
For wider reading, standard books and open resources such as the NCBI Bookshelf help you confirm terms and see more examples. Revise reading and writing csv files in python again a day later and again a week later, because spaced revision improves long-term memory.
This page is part of the Basics of Python Programming for Pharmaceutical Sciences series for first-semester B.Pharm students on B.Pharm Guru, where every chapter has a short summary and a full PDF. Bookmark this page and share it with classmates who are preparing for the same exam.
Related notes: 15. IMPORTING SMALL PHARMACEUTICAL DATASETS AND PERFORMING BASIC DATA ACCESS AND MANIPULATION TASKS, 16. INTRODUCTION TO PANDAS LIBRARY.
Next: 14. UNDERSTANDING STRUCTURED HEALTHCARE DATASETS. Back to the Basics of Python Programming for Pharmaceutical Sciences category.




