我创建了一个名为“坐标”的python列表变量。坐标列表由图形中随机点的x轴和y轴值对组成。 一对中的第一个值表示x轴值,第二个值表示y轴值。坐标列表变量如下所示
coordinates = [(95, 171), (54, 131), (125, 131), (213, 78)]
我实现了一个名为“direction_finder”的函数,通过使用坐标列表变量中的x轴值和y轴值来查找基本方向
该功能的实现步骤如下
1.访问坐标列表变量中每对x轴的值
x1 = coordinates[0][0]
x2 = coordinates[1][0]
x3 = coordinates[2][0]
x4 = coordinates[3][0]
2.访问坐标列表变量中每对中的y轴值
y1 = coordinates[0][1]
y2 = coordinates[1][1]
y3 = coordinates[2][1]
y4 = coordinates[3][1]
3.取x轴和y轴值的度数值
degrees_x1y1 = math.atan2(x1,y1)/math.pi* 180
degrees_x2y2 = math.atan2(x2,y2)/math.pi* 180
degrees_x3y3 = math.atan2(x3,y3)/math.pi* 180
degrees_x4y4 = math.atan2(x4,y4)/math.pi* 180
4.添加if条件以获取最终度数值
if degrees_x1y1 < 0:
degrees_final_x1y1 = 360 + degrees_x1y1
else:
degrees_final_x1y1 = degrees_x1y1
if degrees_x2y2 < 0:
degrees_final_x2y2 = 360 + degrees_x2y2
else:
degrees_final_x2y2 = degrees_x2y2
if degrees_x3y3 < 0:
degrees_final_x3y3 = 360 + degrees_x3y3
else:
degrees_final_x3y3 = degrees_x3y3
if degrees_x4y4 < 0:
degrees_final_x4y4 = 360 + degrees_x4y4
else:
degrees_final_x4y4 = degrees_x4y4
5.为基数方向创建数组
direction_brackets = ["NORTH", "NORTH-EAST","EAST","SOUTH-EAST","SOUTH","SOUTH-WEST","WEST","NORTH-WEST"]
6.四舍五入最终度数值
round_x1y1 = round(degrees_final_x1y1/45)
round_x2y2 = round(degrees_final_x2y2/45)
round_x3y3 = round(degrees_final_x3y3/45)
round_x4y4 = round(degrees_final_x4y4/45)
7.获取最终度数值的最终基数方向
final_direction_x1y1 = direction_brackets[round_x1y1]
final_direction_x2y2 = direction_brackets[round_x2y2]
final_direction_x3y3 = direction_brackets[round_x3y3]
final_direction_x4y4 = direction_brackets[round_x4y4]
8.返回最终的基本方向值
return final_direction_x1y1,final_direction_x2y2, final_direction_x3y3, final_direction_x4y4
9.创建一个名为directions\u list的列表变量,以收集direction\u finder函数返回的最终基数方向值
direction_list = []
direction_list= direction_lookup(coordinates)
10.从'direction_list'变量中获取最终的基本方向
direction_x1y1 = direction_list[0]
direction_x2y2 = direction_list[1]
direction_x3y3 = direction_list[2]
direction_x4y4 = direction_list[3]
在实现代码之后,我必须创建数据库连接以在PHPMyAdmin数据库中存储最终的基数方向。我创建了一个数据库和一个单独的表来存储最终的基数方向。这些STPE的代码如下所示
connection = pymysql.connect(host="localhost", user="root", passwd="", database="directions")
cursor = connection.cursor()
directionsCollect= """Create Table directions_store(
id Int(11) Primary Key Auto_Increment,
cardinal_directions Varchar(255),
)"""
cursor.execute(directionsCollect)
之后,我实现了insert命令来向创建的表插入一个方向
record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
'cardinal_directions': direction_x1y1
}
cursor.execute(record_x1y1)
#commit the connection
connection.commit()
#close the connection
connection.close()
但问题是插入没有发生。错误如下所示
Traceback (most recent call last):
File "floor_plan_detector.py", line 320, in <module>
cursor.execute(record_x1y1)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 762, in _execute_command
packet = prelude + sql[:packet_size-1]
TypeError: can't concat tuple to bytes
该错误表示类型错误。谁能解释一下这里怎么了
你是怎么做到的
在你的台词中:
您正在创建一个元组:类型为tuple(str,dict)的记录 然后将其传递给函数本身
pymysql的cursor.execute然后尝试使用它做一些事情,但它并不知道,因此出现了错误
您应该分别定义查询和参数:
或者,您可以将元组解压缩为args:
相关问题 更多 >
编程相关推荐