Jump to content

Fehler beim Schreiben in die CSV-Datei


Recommended Posts

Ich versuche, die Sensordaten alle 10 Sekunden abzurufen und in einer CSV-Datei zu speichern. 
Ich brauche kontinuierliche Daten. Aber ich erhalte einen Fehler. Den Code habe ich angehängt



HOST = ""
PORT = 4223
UID_tem = "Nwv" # Change XYZ to the UID of your Temperature Bricklet 2.0

import csv

from tinkerforge.ip_connection import IPConnection

def cb_temperature(temperature):
    print("Temperature: " + str(temperature/100.0) + " °C")

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    t = BrickletTemperatureV2(UID_tem, ipcon) # Create device object
    ipcon.connect(HOST, PORT) # Connect to brickd
    t.set_temperature_callback_configuration(1000, False, "x", 0, 0)
    csvfile1= open ('/home/pi/temp1_data.csv','wb')
    output1 = csv.writer(csvfile1,delimiter=';', quotechar='|')
    t.register_callback(t.CALLBACK_TEMPERATURE, cb_temperature)

    input("Press key to exit\n") # Use raw_input() in Python 2
Ich erhalte den folgenden Fehler:

Exception in thread Callback-Processor:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/tinkerforge/ip_connection.py", line 1239, in callback_loop
  File "/home/pi/.local/lib/python3.9/site-packages/tinkerforge/ip_connection.py", line 1219, in dispatch_packet
    cb(unpack_payload(payload, form))
  File "/home/pi/Desktop/testtempcsv.py", line 23, in cb_temperature
ValueError: I/O operation on closed file.


Kann mir jemand freundlicherweise sagen, wie ich das beheben kann? Danke :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...