How do I rectify this error: newline is invalid keyword argument for this function

2024/10/9 12:34:58

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 += 1if 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.temperatureprevious_humidity = result.humiditycounter += 1rightnow = 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!"passtime.sleep(sleep_time)

When I run the script I get the following error: When I run the script I get the following error:

Answer

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

https://en.xdnf.cn/q/118581.html

Related Q&A

How to remove substring from a string in python?

How can I remove the all lowercase letters before and after "Johnson" in these strings? str1 = aBcdJohnsonzZz str2 = asdVJohnsonkkkExpected results are as below:str1 = BJohnsonZ str2 = VJohn…

Try to print frame * and diagonal in python

I try to print * in frame and in diagonal .This is what I did:x=10 y=10 def print_frame(n, m, c):print c * mfor i in range(1, n - 1):print c , *(n-2-i),c, *i , c , cprint c * mprint_frame(10, 10, *)T…

How do I have an object rebound off the canvas border?

I am using the canvas widget from tkinter to create an ellipse and have it move around in the canvas. However when the ellipse comes in contact with the border it gets stuck to wall instead of bouncing…

How to scrape data using next button with ellipsis using Scrapy

I need to continuously get the data on next button <1 2 3 ... 5> but theres no provided href link in the source also theres also elipsis. any idea please? heres my codedef start_requests(self):u…

Execution Code Tracking - How to know which code has been executed in project?

Let say that I have open source project from which I would like to borrow some functionality. Can I get some sort of report generated during execution and/or interaction of this project? Report should…

Python code to ignore errors

I have a code that stops running each time there is an error. Is there a way to add a code to the script which will ignore all errors and keep running the script until completion?Below is the code:imp…

How to match background color of an image with background color of Pygame? [duplicate]

This question already has an answer here:How to convert the background color of image to match the color of Pygame window?(1 answer)Closed 3 years ago.I need to Make a class that draws the character a…

Sharepoint/SOAP - GetListItems ignoring query

Trying to talk from Python to Sharepoint through SOAP.One of the lists I am trying to query contains ID as primary key field.(Field){_RowOrdinal = "0"_FromBaseType = "TRUE"_DisplayN…

python mean between file

I create a list of more than a thousand file (Basically now I have a list with the name of the file) now in order to make the man I thought to do something like this (suppose asch file have 20 lines): …

Sort a dictionary with custom sorting function

I have some JSON data I read from a file using json.load(data_file){"unused_account":{"logins": 0,"date_added": 150},"unused_account2":{"logins": 0,&qu…