首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySql的Python脚本在0x7f79ce81b440>行打印出SRE_Match对象,如何让它打印实际的查询结果?

MySql的Python脚本在0x7f79ce81b440>行打印出SRE_Match对象,如何让它打印实际的查询结果?
EN

Stack Overflow用户
提问于 2018-04-07 23:52:11
回答 1查看 60关注 0票数 0

我正在acars表的消息中搜索纬度/经度坐标。,方向字符组合,如N或NW,后跟5位数字,脚本运行,但它只给我这样的行:

代码语言:javascript
复制
<_sre.SRE_Match object at 0x7f79ce81b440>
<_sre.SRE_Match object at 0x7f79ce81b440>
<_sre.SRE_Match object at 0x7f79ce81b440>
<_sre.SRE_Match object at 0x7f79ce81b440>
<_sre.SRE_Match object at 0x7f79ce81b440>
<_sre.SRE_Match object at 0x7f79ce81b440>

我想看看我的脚本的结果(纬度/经度),我已经尝试了these solutions,当我尝试执行代码时,所有这些结果都导致终端永远暂停。同样,I tried the suggestion for .search() from here 的结果也是一样的。在那里尝试了Findall选项,得到了无穷无尽的PBI D024

我使用基于Unix的VM (ScotchBox)在Windows10PC环境中的终端上运行mysql

代码:

代码语言:javascript
复制
#!/usr/bin/python

# from: http://www.mysqltutorial.org/python-mysql-query

import mysql.connector
import re
import datetime

# Open connection.  My database is named planes.  If you named yours something else
# then change the database= here. 
db = mysql.connector.connect(host='localhost',database='planes',user='root',password='root')

# prepare a cursor "SELECT * from planes.acars limit 10")
cursor = db.cursor ( )

# Do the query -- the limit is just to keep this small, you will not need it

query = ("SELECT msg_text FROM acars "
"WHERE date_time_stamp BETWEEN %s AND %s")
first_of_june = datetime.date(2016, 6,1)
last_of_june = datetime.date(2016, 6,30)
cursor.execute(query, (first_of_june, last_of_june))
# Fetch rows
data = cursor.fetchone ( )
while data is not None:
    coordinates = re.compile ("N|NW|S|SW|E|NE|S|SE^[0-9]{5}")
    print(coordinates.search(str(data)))
    data = cursor.fetchone()

# close cursor
cursor.close ( )

# close connection
db.close ( )

任何帮助都非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-08 00:08:34

"SRE_Match object“表示搜索查询已成功命中搜索模式。

我们可以使用sre_match_object.groups()打印所有匹配的结果

使用搜索结果的可打印版本重写while循环:

代码语言:javascript
复制
while data is not None:
    coordinates = re.compile ("N|NW|S|SW|E|NE|S|SE^[0-9]{5}")
    match_obj = coordinates.search(str(data))
    if match_obj:
        print(match_obj.groups())
    data = cursor.fetchone()

-Ram

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

https://stackoverflow.com/questions/49709143

复制
相关文章

相似问题

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