首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我想通过python3使用全球定位系统数据创建时间戳

我想通过python3使用全球定位系统数据创建时间戳
EN

Stack Overflow用户
提问于 2020-12-30 12:06:16
回答 1查看 52关注 0票数 0

我是python3 Newby。我正在尝试创建一个变量,我可以使用它从adafruit GPS传感器生成GPS时间戳。我最终想把它存储在一个数据库中。当数据插入到表中时,mysql数据库有一个时间戳特性,所以我想要存储来自GPS设备的UTC时间和日期。

好像是我搞错了,弄不清楚。代码挂在下面:

代码语言:javascript
运行
复制
def gpstsRead():  
    gps_ts = '{}/{}/{} {:02}:{:02}:{:02}'.format(
        gps.timestamp_utc.tm_mon,
        gps.timestamp_utc.tm_mday,
        gps.timestamp_utc.tm_year,
        gps.timestamp_utc.tm_hour,
        gps.timestamp_utc.tm_min,
        gps.timestamp_utc.tm_sec,
        )
    return gps_ts

我正在尝试将所有这些都转换为类似于时间戳的格式。错误是这样的:

代码语言:javascript
运行
复制
    Traceback (most recent call last):
  File "/home/pi/ek9/Sensors/GPS/gps-db-insert.py", line 57, in <module>
    gps_ts = gpstsRead()
  File "/home/pi/ek9/Sensors/GPS/gps-db-insert.py", line 20, in gpstsRead
    gps.timestamp_utc.tm_mon,
AttributeError: 'NoneType' object has no attribute 'tm_mon'

我已经确保我使用空格而不是制表符,因为这在过去曾给我带来痛苦。除此之外,我真的不知道。我已经花了几个小时的时间来解决这个问题,但都没有结果。有什么想法吗?谢谢你的建议。

EN

回答 1

Stack Overflow用户

发布于 2020-12-31 04:31:05

感谢所有人的投入。读完这些之后,我决定尝试一下不同的方式。我没有用"def“定义一个变量,而是决定去掉"def”,只创建变量本身。

如下所示:

代码语言:javascript
运行
复制
    # define values
    gps_ts = ('{}-{}-{} {:02}:{:02}:{:02}'.format(
    gps.timestamp_utc.tm_year,
    gps.timestamp_utc.tm_mon,
    gps.timestamp_utc.tm_mday,
    gps.timestamp_utc.tm_hour,
    gps.timestamp_utc.tm_min,
    gps.timestamp_utc.tm_sec,)
    )

这似乎起到了作用。对于任何正在做类似事情的人,我将包括完整的代码。我也明白,就像大多数语言一样,总是有不止一种方法可以完成工作,有些方法比另一些更好,有些则不是。我还在学习。如果有人愿意指出我如何通过做一些不同的或更有效率的事情来完成同样的任务,请随时为我提供学习的机会。再次感谢!

代码语言:javascript
运行
复制
#!/usr/bin/python3
import pymysql
import time
import board
from busio import I2C
import adafruit_gps

i2c = I2C(board.SCL, board.SDA)
gps = adafruit_gps.GPS_GtopI2C(i2c)  # Use I2C interface
gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
gps.send_command(b"PMTK220,1000")
last_print = time.monotonic()

# Open database connection
db = pymysql.connect("localhost", "database", "password", "table")
# prepare a cursor object using cursor() method
cursor = db.cursor()

while True:
    gps.update()
    current = time.monotonic()
    if current - last_print >= 1.0:  # update rate
        last_print = current
        if not gps.has_fix:
            # Try again if we don't have a fix yet.
            print("Waiting for a satellite fix...")
            continue
    # define values
        gps_ts = ('{}-{}-{} {:02}:{:02}:{:02}'.format(
        gps.timestamp_utc.tm_year,
        gps.timestamp_utc.tm_mon,
        gps.timestamp_utc.tm_mday,
        gps.timestamp_utc.tm_hour,
        gps.timestamp_utc.tm_min,
        gps.timestamp_utc.tm_sec,)
        )

        gps_lat = '{}'.format(gps.latitude)

        gps_long = '{}'.format(gps.longitude)

        gps_fix = '{}'.format(gps.fix_quality)

        gps_sat = '{}'.format(gps.satellites)

        gps_alt = '{}'.format(gps.altitude_m)

        gps_speed = '{}'.format(gps.speed_knots)

        gps_track = '{}'.format(gps.track_angle_deg)

        sql = "INSERT into ek9_gps(gps_timestamp_utc, latitude, \
        longitude, fix_quality, number_satellites, gps_altitude, \
        gps_speed, gps_track_angle) \
        values (%s,%s,%s,%s,%s,%s,%s,%s)"

        arg = (gps_ts, gps_lat, gps_long, gps_fix, gps_sat,
        gps_alt, gps_speed, gps_track)

        try:
            # Execute the SQL command
            cursor.execute(sql, arg)
            # Commit your changes in the database
            db.commit()

        except:
            print('There was an error on input into the database')
            # Rollback in case there is any error
            db.rollback()

# disconnect from server
cursor.close()
db.close()

这就是mariadb所展示的:

代码语言:javascript
运行
复制
+----+---------------------+---------------------+----------+-----------+-------------+-------------------+--------------+-----------+-----------------+
| id | datetime            | gps_timestamp_utc   | latitude | longitude | fix_quality | number_satellites | gps_altitude | gps_speed | gps_track_angle |
+----+---------------------+---------------------+----------+-----------+-------------+-------------------+--------------+-----------+-----------------+
| 11 | 2020-12-30 14:14:42 | 2020-12-30 20:14:42 |  xx.xxxx |  -xx.xxxx |           1 |                10 |          232 |         0 |             350 |
| 10 | 2020-12-30 14:14:41 | 2020-12-30 20:14:41 |  xx.xxxx |  -xx.xxxx |           1 |                10 |          232 |         0 |             350 |
|  9 | 2020-12-30 14:14:39 | 2020-12-30 20:14:39 |  xx.xxxx |  -xx.xxxx |           1 |                10 |          232 |         0 |             350 |
|  8 | 2020-12-30 14:14:38 | 2020-12-30 20:14:38 |  xx.xxxx |  -xx.xxxx |           1 |                10 |          232 |         0 |             350 |

成功!再次感谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65501887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档