您现在的位置是:网站首页> 编程资料编程资料
Python如何存储和读取ASCII码形式的byte数据_python_
2023-05-26
355人已围观
简介 Python如何存储和读取ASCII码形式的byte数据_python_
存储和读取ASCII码形式的byte数据
Python可以存byte数据到txt,但不要用str的方式直接存,转成数字列表储存,这样方便读取
L = [] a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000' print(a) for each in a: L.append(int(each)) with open('data.txt','w') as p: p.write(str(L)) print(L) >>> [0, 239, 162, 160, 179, 139, 157, 30, 248, 152, 25, 57, 217, 157, 253, 116, 104, 101, 32, 102, 105, 114, 115, 116, 32, 108, 105, 110, 101, 10, 13, 7, 8, 9, 92, 12, 39, 34, 11, 8, 10, 0] with open('data.txt','r') as p: line = p.readline() print(b''.join([bytes([int(i)]) for i in line[1:-1].split(',')])) >>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe first line\n\r\x07\x08\t\\\x0c\'"\x0b\x08\n\x00'Python ASCII码的获取
ord函数可以获取字符的ASCII码,用法如下:
代码实现:
#ord(‘字符')可以返回该字符的ASCII码 print(ord('a'))运行结果:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- python中的编码和解码及\x和\u问题_python_
- Python实现for循环倒序遍历列表_python_
- Python实现在Excel文件中写入图表_python_
- python学习之列表的运用_python_
- python中文文本切词Kmeans聚类_python_
- Python自动化办公之创建PPT文件_python_
- Python函数值传递引用传递及形式参数和实际参数的区别_python_
- Python中else怎么用?else的用法总结_python_
- python引入requests报错could not be resolved解决方案_python_
- Python枚举类定义和使用方法_python_
