San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 9.12.1 Python Standard Library Module `csv` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The CSV (comma-separated values) format is very common for datasets you may find on the web. \n",
"#### An example CSV file:\n",
"```\n",
"Make,Model,Year,Price\n",
"Honda,Accord,2005,1500\n",
"Toyata,Prius,2010,9000\n",
"Volvo,240 DL,1995,500\n",
"Chevy,Nova,1975,200\n",
"Ford,,2000,400\n",
"```\n",
"#### There is one line per record, and values are separated by commas. Often, the first line contains the column headers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Writing to a CSV File"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Python's `csv` module supports reading and writing CSV files."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('UsedCars.csv', mode='w', newline='') as cars:\n",
" writer = csv.writer(cars)\n",
" writer.writerow(['Make', 'Model', 'Year', 'Price'])\n",
" writer.writerow(['Honda', 'Accord', 2005, 1500])\n",
" writer.writerow(['Toyata', 'Prius', 2010, 9000])\n",
" writer.writerow(['Volvo', '240 DL', 1995, 500])\n",
" writer.writerow(['Chevy', 'Nova', 1975, 200])\n",
" writer.writerow(['Ford', '', 2000, 400])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reading from a CSV File"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('UsedCars.csv', 'r', newline='') as cars:\n",
" reader = csv.reader(cars)\n",
" first = True\n",
"\n",
" for record in reader: \n",
" if first:\n",
" for header in record:\n",
" print(f'{header:<10}', end='')\n",
" print()\n",
" first = False\n",
" else:\n",
" make, model, year, price = record\n",
" print(f'{make:<10}{model:<10}{year:<10}{price:<10}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Commas and Quotes in CSV Data Fields"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### If there are consecutive commas in a CSV record with no value between them, then that is an empty field, as in the Ford line in the example:\n",
"```\n",
"Ford,,2000,400\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Non-numeric values in a CSV files can each be surrounded by double quotes. While writing a CSV file, if a value contains a comma, then enclose the value in quotes. That value will be written with double quotes in the file.\n",
"#### The `csv.writer()` function has parameters that control what character separates values (the delimiter, default is a comma), what the quote character is, and what values to enclose in quotes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('UsedCars.csv', mode='w', newline='') as cars:\n",
" writer = csv.writer(cars, delimiter=',', quotechar='\"', \n",
" quoting=csv.QUOTE_NONNUMERIC)\n",
" writer.writerow(['Make', 'Model', 'Year', 'Price'])\n",
" writer.writerow(['Honda', 'Accord', 2005, 1500])\n",
" writer.writerow(['Toyata', 'Prius', 2010, 9000])\n",
" writer.writerow(['Volvo', '240 DL', 1995, 500])\n",
" writer.writerow(['Chevy', 'Nova', 1975, 200])\n",
" writer.writerow(['Ford', '', 2000, 400])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2023 by Ronald Mak"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}