【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytes<=>str/32-64bit/bytes对象

部署运行你感兴趣的模型镜像

Pickle

>>> import pickle
>>> my_list = [1, 2, 3, 'haha', ['and', 'or']]
>>> pickle_file = open('my_list.pkl', 'wb')
>>> pickle.dump(my_list, pickle_file)
>>> pickle_file.close()
>>> 
>>> 
>>> pickle_file = open('my_list.pkl', 'rb')
>>> my_list2 = pickle.load(pickle_file)
>>> print(my_list2)
[1, 2, 3, 'haha', ['and', 'or']]
>>> 


函数默认参数

>>> def sayWord(name = 'wuyq', word = '快乐学python'):
	print(name + '-->' + word)

	
>>> sayWord()
wuyq-->快乐学python
>>> sayWord('吴英强', '坚持学习,锻炼身体')
吴英强-->坚持学习,锻炼身体
>>> def test(*params):
	print('参数的长度是:',len(params))
	print('第二个参数是:', params[1])

	
>>> test(1, 2, 3, 4)
参数的长度是: 4
第二个参数是: 2
>>> 

函数的参数*args

#coding=utf8
__author__ = 'wuyq'

#当函数的参数不确定时
#*args可以当作可容纳多个变量组成的list或tuple
def fun_var_args(farg, *args):
    print('args: %s'% farg)
    for value in args:
        print('another arg:%s'% value)

fun_var_args(1, 'two', 3, None)

函数的参数**kwargs

def fun_var_kwargs(farg, **kwargs):
    print("args:%s"% farg)
    for key in kwargs:
        print("another keyword arg:%s:%s" %(key, kwargs[key]))

fun_var_kwargs(1, myarg1='two', myarg2=3, myarg3=None)

Bytes<=>str

str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。

>>> b = b'china'
>>> b
b'china'
>>> type(b)
<class 'bytes'>
>>> 
>>> s = b.decode()
>>> s
'china'
>>> bl = s.encode()
>>> bl
b'china'
>>> 

32-64bit

#判断平台是64还是32位
import sys

bits = 0
v = sys.maxsize
while v:
    bits += 1
    v >>= 1
if bits > 32:
    print("64bit")
else:
    print("32bit")
    
#字节序
print(sys.byteorder)

bytes对象

>>> by = b"abcde"
>>> by
b'abcde'
>>> barr = bytearray(by)
>>> barr
bytearray(b'abcde')
>>> barr[0]
97
>>> barr[0] = 98
>>> barr
bytearray(b'bbcde')
>>> 

>>> by = b"abcde"
>>> len(by)
5
>>> by += b"f"
>>> by
b'abcdef'
>>> by[0]
97
>>> by[0] = 98
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    by[0] = 98
TypeError: 'bytes' object does not support item assignment
>>> 

不能混用bytesstrings

不能连接bytes对象和字符串。他们两种不同的数据类型。

也不允许针对字符串中bytes对象的出现次数进行计数,因为串里面根本没有bytes。字符串是一系列的字符序列。也许你是想要先把这些字节序列通过某种编码方式进行解码获得字符串,需要显式地指明它。Python 3不会隐含地将bytes转换成字符串,或者进行相反的操作。

>>> by = b"d"
>>> s = "abcde"
>>> by
b'd'
>>> s
'abcde'
>>> by + s
Traceback (most recent call last):
  File "<pyshell#73>", line 1, in <module>
    by + s
TypeError: can't concat bytes to str
>>> s.count(by)
Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    s.count(by)
TypeError: Can't convert 'bytes' object to str implicitly
>>> s.count(by.decode("ascii"))
1
>>> 

>>> a_string = "深入python"
>>> a_string
'深入python'
>>> len(a_string)
8
>>> by = a_string.encode("uft-8")
Traceback (most recent call last):
  File "<pyshell#79>", line 1, in <module>
    by = a_string.encode("uft-8")
LookupError: unknown encoding: uft-8
>>> by = a_string.encode("utf-8")
>>> by
b'\xe6\xb7\xb1\xe5\x85\xa5python'
>>> len(by)
12
>>> by = a_string.encode("gb18030")
>>> by
b'\xc9\xee\xc8\xebpython'
>>> len(by)
10
>>> by = a_string.encode("big5")
>>> by
b'\xb2`\xa4Jpython'
>>> len(by)
10
>>> roundtrip = by.decode("big5")
>>> roundtrip
'深入python'
>>> a_string
'深入python'
>>> a_string = roundtrip
>>> a_string == roundtrip
True
>>> 

















您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

pip3 install yangsuite-restconf yangsuite-coverage yangsuite-gnmi yangsuite-grpc-telemetry Requirement already satisfied: yangsuite-restconf in /home/user/.local/lib/python3.6/site-packages Requirement already satisfied: yangsuite-coverage in /home/user/.local/lib/python3.6/site-packages Requirement already satisfied: yangsuite-gnmi in /home/user/.local/lib/python3.6/site-packages Requirement already satisfied: yangsuite-grpc-telemetry in /home/user/.local/lib/python3.6/site-packages Requirement already satisfied: yangsuite>=2.8.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-restconf) Requirement already satisfied: djproxy>=2.3.4 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-restconf) Requirement already satisfied: yangsuite-yangtree>=2.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-restconf) Requirement already satisfied: yangsuite-filemanager<2.0.0,>=1.4.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-restconf) Requirement already satisfied: yangsuite-devices<3.0.0,>=2.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-restconf) Requirement already satisfied: yangsuite-netconf<2.0.0,>=1.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: deepdiff==4.0.7 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: regex in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: requests<3.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: markupsafe<2.1.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: elasticsearch<8.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: openpyxl in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: pandas in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: xlsxwriter in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Requirement already satisfied: docker<5.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-coverage) Collecting yang.connector (from yangsuite-gnmi) Downloading https://files.pythonhosted.org/packages/17/3a/4b76b86778cc4f1a2697ed59fd9e7af53bf5a3021269f96ff298178c9398/yang_connector-25.10-py3-none-any.whl Requirement already satisfied: futures in /home/user/.local/lib/python3.6/site-packages (from yangsuite-grpc-telemetry) Requirement already satisfied: packaging>=19.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: python-slugify<5.0.0,>=4.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: docutils in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: django<3 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: python-json-logger>0.1.10 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: whitenoise<5.0.0,>=4.1.2 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: setuptools in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: django-registration<4,>=3 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: beautifulsoup4 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: PyGithub<2.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: wheel in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: appdirs<2.0.0,>=1.4.3 in /home/user/.local/lib/python3.6/site-packages (from yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: six>=1.9.0 in /home/user/.local/lib/python3.6/site-packages (from djproxy>=2.3.4->yangsuite-restconf) Requirement already satisfied: jinja2>=2.10 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-yangtree>=2.0.0->yangsuite-restconf) Requirement already satisfied: xeger==0.3.4 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-yangtree>=2.0.0->yangsuite-restconf) Requirement already satisfied: yamlordereddictloader==0.4.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-yangtree>=2.0.0->yangsuite-restconf) Requirement already satisfied: pyang>1.7.8 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-yangtree>=2.0.0->yangsuite-restconf) Requirement already satisfied: paramiko<3.0,>2.5 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: networkx>=2.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: scp<1.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: gitpython<4.0.0,>=2.1.9 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: netmiko<3.0,>2.4.1 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: pyOpenSSL>=21.0.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: textfsm>=1.1.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: pexpect<5.0.0,>=4.6.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: cryptography>=40.0.2 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: lxml<5.0.0,>=4.3.0 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-netconf<2.0.0,>=1.0.0->yangsuite-coverage) Requirement already satisfied: ncclient>=0.6.4 in /home/user/.local/lib/python3.6/site-packages (from yangsuite-netconf<2.0.0,>=1.0.0->yangsuite-coverage) Requirement already satisfied: ordered-set>=3.1 in /home/user/.local/lib/python3.6/site-packages (from deepdiff==4.0.7->yangsuite-coverage) Requirement already satisfied: jsonpickle>=1.0 in /home/user/.local/lib/python3.6/site-packages (from deepdiff==4.0.7->yangsuite-coverage) Requirement already satisfied: charset-normalizer~=2.0.0; python_version >= "3" in /home/user/.local/lib/python3.6/site-packages (from requests<3.0->yangsuite-coverage) Requirement already satisfied: idna<4,>=2.5; python_version >= "3" in /home/user/.local/lib/python3.6/site-packages (from requests<3.0->yangsuite-coverage) Requirement already satisfied: certifi>=2017.4.17 in /home/user/.local/lib/python3.6/site-packages (from requests<3.0->yangsuite-coverage) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/user/.local/lib/python3.6/site-packages (from requests<3.0->yangsuite-coverage) Requirement already satisfied: et-xmlfile in /home/user/.local/lib/python3.6/site-packages (from openpyxl->yangsuite-coverage) Requirement already satisfied: numpy>=1.15.4 in /home/user/.local/lib/python3.6/site-packages (from pandas->yangsuite-coverage) Requirement already satisfied: pytz>=2017.2 in /home/user/.local/lib/python3.6/site-packages (from pandas->yangsuite-coverage) Requirement already satisfied: python-dateutil>=2.7.3 in /home/user/.local/lib/python3.6/site-packages (from pandas->yangsuite-coverage) Requirement already satisfied: websocket-client>=0.32.0 in /home/user/.local/lib/python3.6/site-packages (from docker<5.0.0->yangsuite-coverage) Requirement already satisfied: protobuf in /home/user/.local/lib/python3.6/site-packages (from yang.connector->yangsuite-gnmi) Requirement already satisfied: grpcio in /home/user/.local/lib/python3.6/site-packages (from yang.connector->yangsuite-gnmi) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/user/.local/lib/python3.6/site-packages (from packaging>=19.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: text-unidecode>=1.3 in /home/user/.local/lib/python3.6/site-packages (from python-slugify<5.0.0,>=4.0.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: sqlparse>=0.2.2 in /home/user/.local/lib/python3.6/site-packages (from django<3->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: confusable-homoglyphs~=3.0 in /home/user/.local/lib/python3.6/site-packages (from django-registration<4,>=3->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: soupsieve>1.2 in /home/user/.local/lib/python3.6/site-packages (from beautifulsoup4->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: pynacl>=1.4.0 in /home/user/.local/lib/python3.6/site-packages (from PyGithub<2.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: deprecated in /home/user/.local/lib/python3.6/site-packages (from PyGithub<2.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: pyjwt>=2.0 in /home/user/.local/lib/python3.6/site-packages (from PyGithub<2.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: pyyaml in /home/user/.local/lib/python3.6/site-packages (from yamlordereddictloader==0.4.0->yangsuite-yangtree>=2.0.0->yangsuite-restconf) Requirement already satisfied: bcrypt>=3.1.3 in /home/user/.local/lib/python3.6/site-packages (from paramiko<3.0,>2.5->yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: decorator<5,>=4.3 in /home/user/.local/lib/python3.6/site-packages (from networkx>=2.0->yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: gitdb<5,>=4.0.1 in /home/user/.local/lib/python3.6/site-packages (from gitpython<4.0.0,>=2.1.9->yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: typing-extensions>=3.7.4.0; python_version < "3.8" in /home/user/.local/lib/python3.6/site-packages (from gitpython<4.0.0,>=2.1.9->yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: pyserial in /home/user/.local/lib/python3.6/site-packages (from netmiko<3.0,>2.4.1->yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: ptyprocess>=0.5 in /home/user/.local/lib/python3.6/site-packages (from pexpect<5.0.0,>=4.6.0->yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: cffi>=1.12 in /home/user/.local/lib/python3.6/site-packages (from cryptography>=40.0.2->yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: importlib-metadata; python_version < "3.8" in /home/user/.local/lib/python3.6/site-packages (from jsonpickle>=1.0->deepdiff==4.0.7->yangsuite-coverage) Requirement already satisfied: wrapt<3,>=1.10 in /home/user/.local/lib/python3.6/site-packages (from deprecated->PyGithub<2.0->yangsuite>=2.8.0->yangsuite-restconf) Requirement already satisfied: smmap<6,>=3.0.1 in /home/user/.local/lib/python3.6/site-packages (from gitdb<5,>=4.0.1->gitpython<4.0.0,>=2.1.9->yangsuite-filemanager<2.0.0,>=1.4.0->yangsuite-restconf) Requirement already satisfied: pycparser in /home/user/.local/lib/python3.6/site-packages (from cffi>=1.12->cryptography>=40.0.2->yangsuite-devices<3.0.0,>=2.0.0->yangsuite-restconf) Requirement already satisfied: zipp>=0.5 in /home/user/.local/lib/python3.6/site-packages (from importlib-metadata; python_version < "3.8"->jsonpickle>=1.0->deepdiff==4.0.7->yangsuite-coverage) Installing collected packages: yang.connector Exception: Traceback (most recent call last): File "/usr/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main status = self.run(options, args) File "/usr/lib/python3.6/site-packages/pip/commands/install.py", line 365, in run strip_file_prefix=options.strip_file_prefix, File "/usr/lib/python3.6/site-packages/pip/req/req_set.py", line 789, in install **kwargs File "/usr/lib/python3.6/site-packages/pip/req/req_install.py", line 854, in install strip_file_prefix=strip_file_prefix File "/usr/lib/python3.6/site-packages/pip/req/req_install.py", line 1069, in move_wheel_files strip_file_prefix=strip_file_prefix, File "/usr/lib/python3.6/site-packages/pip/wheel.py", line 345, in move_wheel_files clobber(source, lib_dir, True) File "/usr/lib/python3.6/site-packages/pip/wheel.py", line 287, in clobber ensure_dir(dest) # common for the 'include' path File "/usr/lib/python3.6/site-packages/pip/utils/__init__.py", line 83, in ensure_dir os.makedirs(path) File "/usr/lib64/python3.6/os.py", line 210, in makedirs makedirs(head, mode, exist_ok) File "/usr/lib64/python3.6/os.py", line 220, in makedirs mkdir(name, mode) PermissionError: [Errno 13] Permission denied: '/usr/local/lib64/python3.6'
最新发布
11-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值