Python 3.4 语法错误及修复方法
Python 3.4 Syntax error and how to fix it
虽然在 Python 3.6 上接受了以下行,但在 Python 3.4 上我收到语法错误:
struct.pack_into('q' * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
其中 GeoFence_lon_list 是声明为的数组:
Geo_Fence_list = []
GeoFence_lat_list = []
GeoFence_lon_list = []
这里有更多代码需要查看:
if (Polygon_available_size == 0):
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
elif (int(Polygon_available_size) > 0):
geo_fence_size = struct.calcsize('q' * len(GeoFence_lat_list)*2)
#print("geo_fence_size", geo_fence_size)
workzone_size = workzone_size + geo_fence_size
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
struct.pack_into('q', * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
GeoFence_lat_list.clear()
GeoFence_lon_list.clear()
packed_flag = 1
Python 3.4 每次调用只允许一个未打包的参数。
您的原始代码使用多次解包只是将多个列表附加到字节缓冲区中。您可以简单地通过多次调用 struct.pack_into()
来实现相同的目的,并为每个部分设置适当的偏移量。
start = 264
struct.pack_into(str(len(GeoFence_lat_list)) + 'q', buff, start, *GeoFence_lat_list);
start += 8 * len(GeoFence_lat_list)
struct.pack_into(str(len(GeoFence_lon_list)) + 'q', buff, start, *GeoFence_lon_list);
虽然在 Python 3.6 上接受了以下行,但在 Python 3.4 上我收到语法错误:
struct.pack_into('q' * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
其中 GeoFence_lon_list 是声明为的数组:
Geo_Fence_list = []
GeoFence_lat_list = []
GeoFence_lon_list = []
这里有更多代码需要查看:
if (Polygon_available_size == 0):
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
elif (int(Polygon_available_size) > 0):
geo_fence_size = struct.calcsize('q' * len(GeoFence_lat_list)*2)
#print("geo_fence_size", geo_fence_size)
workzone_size = workzone_size + geo_fence_size
buff = ctypes.create_string_buffer(workzone_size)
struct.pack_into('q' * 33, buff, 0, Speed_limit, Speed_Zone_lat, Speed_Zone_longi, Speed_Zone_heading_radians,
Speed_Zone_ITIS_CODE, Speed_Zone_2_lat, Speed_Zone_2_longi, Speed_Zone_2_heading_radians, Speed_Zone_2_ITIS_CODE,G20_lat, G20_longi,
G20_heading_radians, G20_ITIS_CODE,W20_lat, W20_longi, W20_heading_radians ,W20_ITIS_CODE,W21_5BR_lat,W21_5BR_longi, W21_5BR_heading_radians,
W21_5BR_ITIS_CODE,W21_5AR_lat,W21_5AR_longi, W21_5AR_heading_radians, W21_5AR_ITIS_CODE,First_Taper_lat,First_Taper_longi,
Last_Taper_lat, Last_Taper_longi, 2020, 2456,60, Polygon_available_size)
struct.pack_into('q', * len(GeoFence_lat_list)*2,buff,264,*GeoFence_lat_list, *GeoFence_lon_list)
GeoFence_lat_list.clear()
GeoFence_lon_list.clear()
packed_flag = 1
Python 3.4 每次调用只允许一个未打包的参数。
您的原始代码使用多次解包只是将多个列表附加到字节缓冲区中。您可以简单地通过多次调用 struct.pack_into()
来实现相同的目的,并为每个部分设置适当的偏移量。
start = 264
struct.pack_into(str(len(GeoFence_lat_list)) + 'q', buff, start, *GeoFence_lat_list);
start += 8 * len(GeoFence_lat_list)
struct.pack_into(str(len(GeoFence_lon_list)) + 'q', buff, start, *GeoFence_lon_list);