【问题标题】:Error ------ unexpected EOF while parsing错误 ------ 解析时出现意外的 EOF
【发布时间】:2019-01-09 22:23:20
【问题描述】:

我在我的树莓派 3 上使用最新的 raspbian 软件在 Python 3(IDLE) 上运行此代码。使用此代码,我试图通过 ds18b20 传感器获取温度数据,并将相同的数据发送到我创建的 mysql 数据库。

从try:到if connection.is_connected()结束,我正在建立与mysql数据库的连接。

从 if os.system('modprobe w1-gpio') 返回 temp_c,我通过 ds18b20 传感器获取温度数据。

从 Whiletrue 到我的代码末尾,我尝试将温度数据发送到名为 TAB_CLASSROOM 的特定表中。

非常感谢您的帮助!

这是完整的错误!:

Traceback (most recent call last):
  File "/home/pi/Desktop/mysqlfinal1test.py", line 74
    db.close()
              ^
SyntaxError: unexpected EOF while parsing

这是包含mysql代码的python:

import os
import glob
import time
import MySQLdb
import datetime
import mysql.connector
from mysql.connector import Error

i = datetime.datetime.now()

try:
    connection = mysql.connector.connect(host='127.0.0.1',
                             database='temp_pi',
                             user='root',
                             password='test')

    if connection.is_connected():
       db_Info = connection.get_server_info()
       print("Connected to MySQL database... MySQL Server version on ",db_Info)
       cursor = connection.cursor()
       cursor.execute("select database();")
       record = cursor.fetchone()
       print ("Your connected to - ", record)

    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')

    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')[0]
    device_file = device_folder + '/w1_slave'

    def read_temp_raw():
        f = open(device_file, 'r')
        lines = f.readlines()
        f.close()
        return lines

    def read_temp():
        lines = read_temp_raw()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = read_temp_raw()
        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp_string = lines[1][equals_pos+2:]
            temp_c = float(temp_string) / 1000.0
            temp_f = temp_c * 9.0 / 5.0 + 32.0
            return temp_c  

        while True:
              print("recording data into database(period = 5s.)....press ctrl+Z to stop!")

              valT = str(read_temp())

              year = str(i.year)
              month = str(i.month)
              day = str(i.day)
              date = day + "-" + month + "-" + year

              hour = str(i.hour)
              minute = str(i.minute)
              second = str(i.second)
              timestr = hour + ":" + minute + ":" + second

              try:
                 cur.execute("""INSERT INTO TAB_CLASSROOM(temp_c,T_Date,T_Time) VALUES(%s,%s,%s)""",(valT,date,time))
                 db.commit()
              except:
                 db.rollback()

              time.sleep(10)

        cur.close()  
        db.close()

【问题讨论】:

    标签: python mysql-python eof


    【解决方案1】:

    大部分发布的程序都是一个巨大的try 块,没有except 子句。因此,当解析器到达文件底部时,它无法完成打开的控制块。

    try 在第 11 行;我们在第 74 行之后用完了输入,没有任何 except 对应于 try。

    我怀疑你的缩进有问题(除其他外),因为这个try 包含两个函数定义。不过,您有两个 try 语句,并且只有一个 except。

    【讨论】:

    • 我确实在末尾添加了一个 except 子句。这就是为什么我很困惑@Prune
    • 您有一个except,它位于read_temp 的while 循环内。第 11 行的 try 没有 except。
    • 啊谢谢错误消失了!我更新了我的代码。但是现在它不会在我遇到的异常错误之后运行其余的代码。
    • (1) 那是一个单独的问题,需要一个单独的问题; (2)您没有发布违规代码,因此我们无法诊断问题; (3) 只是一个狂野的镜头:您处理异常了吗?如果不是……那么,请阅读try - except 构造的文档。
    • 好吧,你回答了我的问题!大声笑,但我现在有一个不同的问题。谢谢你的帮助!关于在建立与数据库的连接后如何解决 python 无法运行其余代码的问题的任何提示? @Prune
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2013-04-25
    • 2021-03-21
    相关资源
    最近更新 更多