Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Programming for Arduino

You're reading from   Python Programming for Arduino Develop practical Internet of Things prototypes and applications with Arduino and Python

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781783285938
Length 400 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Pratik Desai Pratik Desai
Author Profile Icon Pratik Desai
Pratik Desai
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Python and Arduino FREE CHAPTER 2. Working with the Firmata Protocol and the pySerial Library 3. The First Project – Motion-triggered LEDs 4. Diving into Python-Arduino Prototyping 5. Working with the Python GUI 6. Storing and Plotting Arduino Data 7. The Midterm Project – a Portable DIY Thermostat 8. Introduction to Arduino Networking 9. Arduino and the Internet of Things 10. The Final Project – a Remote Home Monitoring System 11. Tweet-a-PowerStrip Index

Using CSV files to store data


Now you know methods to open, manipulate, and close files using Python. In the previous examples, we used the Python interpreter and string data to get familiar with these methods. But when it comes to saving a large number of numerical values from sensor data, the comma separated values (CSV) file format is one of the most widely used file formats other than text. As the name states, values are separated and stored using commas or other delimiters such as a space or tab. Python has a built-in module to deal with CSV files.

To begin with, use the following code snippet to create a Python file and run your first CSV program:

import csv
data = [[1, 2, 3], ['a', 'b', 'c'], ['Python', 'Arduino', 'Programming']]

with open('example.csv', 'w') as f:
  w = csv.writer(f)
  for row in data:
    w.writerow(row)

You can also open the csvWriter.py file from this chapter's code folder, which contains the same code. After executing the code, you will be able to find a file named...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image