我目前正在使用raspberry pi并使用DHT11每秒读取温度和湿度值.我必须将这些值实时保存到数据库中.这是我每秒钟显示传感器数据的代码,我不知道如何在excel中保存数据/结果.

import RPi.GPIO as GPIO

import dht11

import time

import datetime

import os

# initialize GPIO

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.cleanup()

instance = dht11.DHT11(pin=dht11_pin)

while True:

cnt += 1

if cnt%limit_sec == 0 or cnt == 1:

result = instance.read()

if result.is_valid():

if previous_temperature != result.temperature or previous_humidity != result.humidity:

previous_temperature = result.temperature

previous_humidity = result.humidity

counter += 1

rightnow = datetime.datetime.now()

if result.humidity>=40:

print(str(counter)+". Last valid input: " )

print("Date: " + rightnow.strftime("%d/%m/%Y"))

print("Time: " + rightnow.strftime("%H:%M:%S"))

print("Status: Your plant is on the good condition.")

print("Temperature: %d C" % result.temperature)

print("Humidity: %d %%" % result.humidity)

print("*******************************************")

else:

print(str(counter)+". Last valid input: " )

print("Date: " + rightnow.strftime("%d/%m/%Y"))

print("Time: " + rightnow.strftime("%H:%M:%S"))

print("Status: Your plant is on the bad condition. Please open the water supply.")

print("Temperature: %d C" % result.temperature)

print("Humidity: %d %%" % result.humidity)

print("*******************************************")

else:

print "Invalid result!"

pass

time.sleep(sleep_time)

解决方法:

首先是导入csv模块然后使用

使用open("file_name.csv’,’w’,newline =”)作为csvfile:

writer = csv.DictWriter(csvfile,fieldnames = field_names)

field_names只是列的键值

writer.writerow(

{"日期’:’日期’,’时间’:’时间’,

"状态’:’状态’,’温度’:’温度’,’湿度’:’湿度’})为excel文件写标题

writer.writerow(

{"Date’:rightnow.strftime(“%d /%m /%Y”),’Time’:rightnow.strftime(“%H:%M:%S”),

"Status’:status,’Temperature’:result.temperature,’Humidity’:result.humidity})根据field_names中的键值在csv文件中写入数据

完整代码:

import RPi.GPIO as GPIO

import dht11

import time

import datetime

import csv

import os

# initialize GPIO

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.cleanup()

instance = dht11.DHT11(pin=dht11_pin)

with open('file_name.csv', 'w', newline='') as csvfile:

field_names = ['Date', 'Time', 'Status', 'Temperature', 'Humidity']

writer = csv.DictWriter(csvfile, fieldnames=field_names)

writer.writerow(

{'Date': 'Date', 'Time': 'Time',

'Status': 'Status', 'Temperature': 'Temperature', 'Humidity': 'Humidity'})

while True:

cnt += 1

if cnt%limit_sec == 0 or cnt == 1:

result = instance.read()

if result.is_valid():

if previous_temperature != result.temperature or previous_humidity != result.humidity:

previous_temperature = result.temperature

previous_humidity = result.humidity

counter += 1

rightnow = datetime.datetime.now()

if result.humidity>=40:

status = 'Your plant is on the good condition.'

print(str(counter)+". Last valid input: " )

print("Date: " + rightnow.strftime("%d/%m/%Y"))

print("Time: " + rightnow.strftime("%H:%M:%S"))

print("Status: Your plant is on the good condition.")

print("Temperature: %d C" % result.temperature)

print("Humidity: %d %%" % result.humidity)

print("*******************************************")

else:

status = 'Your plant is on the bad condition. Please open the water supply.'

print(str(counter)+". Last valid input: " )

print("Date: " + rightnow.strftime("%d/%m/%Y"))

print("Time: " + rightnow.strftime("%H:%M:%S"))

print("Status: Your plant is on the bad condition. Please open the water supply.")

print("Temperature: %d C" % result.temperature)

print("Humidity: %d %%" % result.humidity)

print("*******************************************")

writer.writerow(

{'Date': rightnow.strftime("%d/%m/%Y"), 'Time': rightnow.strftime("%H:%M:%S"),

'Status': status, 'Temperature':result.temperature, 'Humidity': result.humidity})

else:

print "Invalid result!"

pass

time.sleep(sleep_time)

第一个writer.writerow将是你的标题,而field_names只是用作将数据填充到特定列的键

存储你的状态=”并将它放在writer.writerow()等.

标签:raspberry-pi3,sensor,soil,python,github

来源: https://codeday.me/bug/20191008/1873950.html

python输出数据到excel-如何使用python将传感器数据输出保存到excel中相关推荐

  1. python 爬虫抓取网页数据导出excel_Python爬虫|爬取起点中文网小说信息保存到Excel...

    前言: 爬取起点中文网全部小说基本信息,小说名.作者.类别.连载\完结情况.简介,并将爬取的数据存储与EXCEL表中 环境:Python3.7 PyCharm Chrome浏览器 主要模块:xlwt ...

  2. 用python提取发票扫描件常用的10多个发票信息保存到excel表

    用python提取发票扫描件常用的10多个发票信息 #输出所有文件和文件夹 如何将发票扫描将中常用的10-20个信息提取到excel表格中,用python提取是不错的方法. 1.用python提取发票 ...

  3. python提取pdf表格数据并保存到excel中

    pdfplumber操作pdf文件 python开源库pdfplumber,可以较为方便地获取pdf的各种信息,包含pdf的基本信息(作者.创建时间.修改时间-)及表格.文本.图片等信息,基本可以满足 ...

  4. python数据采集 爬虫 生意宝_Python爬虫实战 :批量采集股票数据,并保存到Excel中...

    小编说:通过本文,读者可以掌握分析网页的技巧.Python编写网络程序的方法.Excel的操作,以及正则表达式的使用.这些都是爬虫项目中必备的知识和技能.本文选自<Python带我起飞>. ...

  5. python提取txt数据到excel_python 读取txt中每行数据,并且保存到excel中的实例

    使用xlwt读取txt文件内容,并且写入到excel中,代码如下,已经加了注释. 代码简单,具体代码如下: # coding=utf-8 ''' main function:主要实现把txt中的每行数 ...

  6. python逐行读取txt写入excel_python 读取txt中每行数据,并且保存到excel中的实例

    使用xlwt读取txt文件内容,并且写入到excel中,代码如下,已经加了注释. 代码简单,具体代码如下: # coding=utf-8 ''' main function:主要实现把txt中的每行数 ...

  7. python 发票信息提取_Python提取发票内容保存到Excel.md

    Python提取PDF发票内容保存到Excel --- 摘要:这篇文章介绍如何把发票内容提取出来保存到Excel中.文章分为两个部分,第一部分程序用法,第二部分介绍代码. --- 作者:yooongc ...

  8. Python实现分析pdf或者Word形式简历,并且保存到Excel中

    Python实现分析当前文件夹里面所有的pdf或者Word形式简历,并且保存到Excel中 # -*- coding:utf-8 -*-#作者:公众号:湾区人工智能 #功能:实现分析pdf或者Word ...

  9. Python爬取中国大学排名,并且保存到excel中

    前言 以下文章来源于数据分析和Python ,作者冈坂日川 今天发的是python爬虫爬取中国大学排名,并且保存到excel中,当然这个代码很简单,我用了半小时就写完了,我的整体框架非常清晰,可以直接 ...

  10. python爬虫爬取豆瓣电影排行榜并通过pandas保存到Excel文件当中

    我们的需求是利用python爬虫爬取豆瓣电影排行榜数据,并将数据通过pandas保存到Excel文件当中(步骤详细) 我们用到的第三方库如下所示: import requests import pan ...

最新文章

  1. flask 和 ajax 实例
  2. python-包package
  3. 网站商务通如何导出查看历史聊天纪录
  4. Vue 脚手架||Vue 脚手架的基本用法
  5. iOS之路9-#import 与#include和 @class的区别
  6. 地理生物高考成绩查询2021,2021北京中考地理生物成绩查询时间【已公布】
  7. Vue.js视频教程
  8. Opencv之通过url抓取图片并通过opencv可视化
  9. java项目关联关系_Mybatis一对多关联关系映射实现过程解析
  10. 数学建模中的常见模型
  11. 获取高程数据以及转灰度图和裁剪操作
  12. 13.计蒜客ACM题库.A1147 结果填空:礼物盒
  13. Redis 雪崩、穿透、击穿
  14. Matlab imcrop 与 opencv ROI
  15. @Resource详解-代码示例
  16. 20行Python代码,轻松提取PPT文字到Word!
  17. mapbox-gl开发教程(二):地图主要配置参数说明
  18. QQ是怎么实现通讯的
  19. 360周鸿祎:互联网成功十大案例
  20. MySQL-MySQL连接,创建,删除数据库database

热门文章

  1. Linux常用服务安装部署
  2. bzoj3920: Yuuna的礼物(莫队+分块套分块)
  3. 理解First Chance和Second Chance避免单步调试
  4. Floyd_Warshall POJ 1847 Tram
  5. 多返回值函数的编写方法
  6. 【转】基本概念:过拟合、修剪、假正、假负
  7. [flex]报错,Resource Path Location Type 源路径条目“… Unknown Flex 问题
  8. 以下选项不是python打开方式的是-关于文件的打开方式,以下选项中描述正确的是...
  9. python使用方法-python-tkinter使用方法——转载(一)
  10. vscode使用教程python-使用VS Code开发Python