字典
struct打包
局部变量全局变量声明
判断数字和字符
**
1.字典
def HeartReq(self, **kwargs):
'心跳请求'
print(len(**kwargs)) #----------->代码修正 print(len(kwargs))
Error:
Traceback (most recent call last):
File "D:\Step1_data_send1\B1\T2.py", line 515, in <module>
a_str = hadwagestrt.hardware_str_put(dict_time)
File "D:\Step1_data_send1\B1\T2.py", line 461, in hardware_str_put
one_data = self.hardware_str(**strlist)
File "D:\Step1_data_send1\B1\T2.py", line 70, in hardware_str
ret_str=self.HeartReq(**kwargs)
File "D:\Step1_data_send1\B1\T2.py", line 98, in HeartReq
print(len(**kwargs))
TypeError: len() takes no keyword arguments 关键字
2.打包
python.struct (pack unpack)
①一个格式化字符串(format strings),用来规定转化的方法和
格式。
transcoding_type = {'心跳请求': '>IIII',
'心跳请求1': '>III', #4,4,4,4
'心跳请求2': '>IIIII', # 4,4,4,4
'心跳请求3': '>iIII', # 4,4,4,4}
def pack(fmt, *args): # known case of _struct.pack
"""
pack(format, v1, v2, ...) -> bytes
Return a bytes object containing the values v1, v2, ... packed according
to the format string. See help(struct) for more on format strings.
"""
return b""
**②大小端之分
小端是低位在内存低地址端,反之大端 就是高位在内存低地址
import struct**
buffer = struct.pack("<5shb", b'111', 2, 30)
print(repr(buffer))
print(struct.unpack("<5shb",buffer))
buffer = struct.pack(">5shb", b'111', 2, 30)
print(repr(buffer))
print(struct.unpack(">5shb",buffer))
b'111\x00\x00\x02\x00\x1e'
(b'111\x00\x00', 2, 30)
b'111\x00\x00\x00\x02\x1e'
(b'111\x00\x00', 2, 30)
3.局部变量全局变量声明
UnboundLocalError: local variable 'list' referenced before assignment
运行时报错:UnboundLocalError:局部变量’list’在赋值前被引用
原因:python解释器不知道这是全局还是局部变量。
解决:
1.声明全局变量 global一下
2.当做局部变量来用,只在函数内部使用,声明一下就好
list = []
3.return
4.判断数字还是字符
isdigit() 但是 如果负数 会认为是字符
if len(kwargs) == 17:
# if self.is_number(str(self.code)):
if self.code.isdigit():
if self.code >= 0:
print(self.code, self.length, EntrustDate, ExternSysNo, ExternReportNo, FundCode,
StockAccount, ExchangeType, SecurityCode, EntrustDirection, EntrustAmount, PriceType, EntrustPrice,
EntrustBalance, NodeId, BusinessID, Reserved)
print(self.transcoding_type['委托请求'])
return struct.pack(self.transcoding_type['委托请求'], self.code, self.length, EntrustDate,ExternSysNo, ExternReportNo, FundCode, StockAccount, ExchangeType, SecurityCode,EntrustDirection, EntrustAmount, PriceType, EntrustPrice, EntrustBalance, NodeId, BusinessID, Reserved)
else:
print(self.code, self.length, EntrustDate, ExternSysNo, ExternReportNo, FundCode,
StockAccount, ExchangeType, SecurityCode, EntrustDirection, EntrustAmount, PriceType, EntrustPrice,
EntrustBalance, NodeId, BusinessID, Reserved)
print(self.transcoding_type['委托请求4'])
return struct.pack(self.transcoding_type['委托请求4'], self.code, self.length, EntrustDate,ExternSysNo, ExternReportNo, FundCode, StockAccount, ExchangeType, SecurityCode,EntrustDirection, EntrustAmount, PriceType, EntrustPrice, EntrustBalance, NodeId, BusinessID, Reserved)
else:
print(self.code, self.length, EntrustDate, ExternSysNo, ExternReportNo, FundCode,
StockAccount, ExchangeType, SecurityCode, EntrustDirection, EntrustAmount, PriceType,
所以只好再写一个判断啊这样
这样就ok 了
def is_number(self,s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)