如何纠正此错误:“换行"是此函数的无效关键字参数 [英] How do I rectify this error: "newline" is invalid keyword argument for this function

查看:62
本文介绍了如何纠正此错误:“换行"是此函数的无效关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working with raspberry pi and using DHT11 to read temperature and humidity values every second. I have to save these values into a database in real time. Here's my code that showing sensor data every second.

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)

运行脚本时出现以下错误:

When I run the script I get the following error:

推荐答案

检查您是否使用 Python 3.可能是您使用的 Python 以前版本不支持换行符(Python 2 及更低版本).在 Python 3 上添加了换行参数.https://docs.python.org/3/library/functions.html#open

Check if you are using Python 3. It might be that you are using a previous version of Python that does not support newline (Python 2 and below). Newline argument was added on Python 3. https://docs.python.org/3/library/functions.html#open

这篇关于如何纠正此错误:“换行"是此函数的无效关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆