【测开简单编码问题记录】

1.字典

2.struct打包

3.局部变量全局变量声明

4.判断数字和字符调用 unicodedata.numeric 不用isdigit

5.暂停程序运行 继续

6.django linux 部署启动报错
**

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

**5. 暂停 继续执行程序 **

win:    os.system('pause')

linux:    os.system('read')

6. Django linux部署启动报错

1.报错 manage.py from exc 出问题

用python3 manage.py runserver 用python3 虚拟环境

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值