赞
踩
>>> b=b'abc' >>> type(b) <class 'bytes'> >>> b=b"abc" >>> type(b) <class 'bytes'> >>>content = bytes([1, 2, 3, 4]) >>> content b'\x01\x02\x03\x04' >>> type(content) <class 'bytes'> bytearray(b'\x01\x02\x03\x04') >>> print(content) bytearray(b'\x01\x02\x03\x04') >>> content[1]=5 >>> content bytearray(b'\x01\x05\x03\x04') >>> content_array = bytearray(content) >>> type(content_array) <class 'bytearray'> >>> content = bytes(content_array) >>> type(content) <class 'bytes'>
>>> nums=['ww','22','2s']
>>> nums
['ww', '22', '2s']
>>> type(nums)
<class 'list'>
>>> name = "mike"
>>> type(name)
<class 'str'>
>>> name
'mike'
>>> name_list = list(name)
>>> name_list
['m', 'i', 'k', 'e']
>>> nums = [1, 2, 3, 4]
>>> nums_list = [str(x) for x in nums]
>>> nums_list
['1', '2', '3', '4']
>>> str().join(str_nums)
'1234'
str和bytes都是不可变类型,并且都是连续内存。虽然它们的结构是相同的,甚至存储的数据也可以完全一样,但是str是字符流,表现形式是字符串,而bytes是字节流,表现形式是数字序列。encode和decode默认是以utf-8编码来进行转换的,可以指定gbk编码。
>>> name = "mike" >>> name_bytes = name.encode() >>> name_bytes b'mike' >>> name = name_bytes.decode() >>> name 'mike' >>> text = "我是谁" >>> text.encode('gbk') b'\xce\xd2\xca\xc7\xcb\xad' >>> text.encode('gbk') b'\xce\xd2\xca\xc7\xcb\xad' >>> text_bytes = text.encode('gbk') >>> text_bytes b'\xce\xd2\xca\xc7\xcb\xad' >>> new_text = text_bytes.decode('gbk') >>> new_text '我是谁' >>> text='1234' >>> print(bytes(text, 'utf-8')) b'1234'
# int转bytes
val = 0x123456789abc
bytes_val = val.to_bytes(4, "big")
bytes_val = val.to_bytes(5, "little")
# bytes转int
val = int.from_bytes(bytes_val, "little")
数字转换为字符串主要是通过str类的成员函数format或f字符串语法来完成。如果是简单的转换,也可以通过初始化函数来进行转换。
>>> name = 'Peter' >>> age = 23 >>> print('{} is {} years old'.format(name, age)) Peter is 23 years old >>> print(f'{name} is {age} years old') Peter is 23 years old >>> a = 123.456 >>> f"a if {a:8.2f}" 'a if 123.46' >>> print(hex(a)) 0x1234 >>> a=1234 >>> print(hex(a)) 0x4d2 >>> a=1234 >>> print(str(a)) 1234 >>> a=0x1234 >>> print(str(a)) 4660
>>> phone_number = "13988888888"
>>> int(phone_number, 10)
13988888888
>>> int(phone_number)
13988888888
>>> hex_str = "0x1234"
>>> int(hex_str, 16)
4660
os.mknod("test.txt")
open("test.txt", w)
# 创建单目录
os.mkdir("test")
# 创建多层目录
os.makedirs(r"d:\abc\def")
# pathlib创建目录
pathlib.Path("temp/").mkdir(parents=True, exist_ok=True)
os.remove("test.txt")
os.removedirs("test")
os.rmdir("test")
import shutil
shutil.rmtree('/path/to/your/dir/')
>>> import pathlib
>>> p = pathlib.Path('.')
>>> [x for x in p.iterdir() if x.is_dir()]
files = pathlib.Path(".").rglob("*.py")
for file in files:
print(file)
p = pathlib.Path('.')
[print(x) for x in p.iterdir()]
with open("scan.bin", "rb+") as bin_file:
content = bin_file.read()
content_array = bytearray(content)
content_array[2] = 5
with open("new.bin", "wb") as new_file:
new_file.write(content_array)
# utf-8编码
with open("test6.py", "r", encoding='utf-8') as file:
text = file.read()
print(text)
# gbk编码
with open("a.c", "r", encoding='gb18030') as file:
text = file.read()
print(text)
>>> import pathlib
>>> p = pathlib.Path('a.bin')
>>> p.write_bytes(b'Binary file contents')
20
>>> p.read_bytes()
b'Binary file contents'
>>> p = pathlib.Path('my_text_file.txt')
>>> p.write_text('Text file contents')
18
>>> p.read_text()
'Text file contents'
with open("test6.py", "r", encoding='utf-8') as file:
print(file.readline()) # 读取第1行
print(file.readline()) # 读取第2行
# 遍历输出所有文本
with open("test6.py", "r", encoding='utf-8') as file:
lines = file.readlines()
[print(line) for line in lines]
#coding=utf-8 import sys lines = [] lines.append("#pragma once\n") lines.append("\n") lines.append("const static char binArr[] = {\n") with open(sys.argv[1], "rb") as bin_file: content = bin_file.read() size = len(content) per_line_size = 16 for n in range(0, size): if (0 == n%per_line_size): line = [" "] if (n == size-1): line.append(f"0x{content[n]:02x}") lines.extend(line) else: line.append(f"0x{content[n]:02x}, ") if (15 == n % per_line_size): line.append("\n") lines.extend(line) line.clear() with open(sys.argv[2], "w") as file: lines.append("};\n"); file.writelines(lines)
效果如下图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。