sy1.proto文件

syntax = "proto2";
message stuff
{
required int32 stuff_ID = ;
required string stuff_Name = ;
optional int32 stuff_Num = ; }

利用编译器转化成sy1_pb2.py文件

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: sy1.proto import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor.FileDescriptor(
name='sy1.proto',
package='',
serialized_pb=_b('\n\tsy1.proto\"@\n\x05stuff\x12\x10\n\x08stuff_ID\x18\x01 \x02(\x05\x12\x12\n\nstuff_Name\x18\x02 \x02(\t\x12\x11\n\tstuff_Num\x18\x03 \x01(\x05')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR) _STUFF = _descriptor.Descriptor(
name='stuff',
full_name='stuff',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='stuff_ID', full_name='stuff.stuff_ID', index=0,
number=1, type=5, cpp_type=1, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='stuff_Name', full_name='stuff.stuff_Name', index=1,
number=2, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='stuff_Num', full_name='stuff.stuff_Num', index=2,
number=3, type=5, cpp_type=1, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=13,
serialized_end=77,
) DESCRIPTOR.message_types_by_name['stuff'] = _STUFF stuff = _reflection.GeneratedProtocolMessageType('stuff', (_message.Message,), dict(
DESCRIPTOR = _STUFF,
__module__ = 'sy1_pb2'
# @@protoc_insertion_point(class_scope:stuff)
))
_sym_db.RegisterMessage(stuff) # @@protoc_insertion_point(module_scope)

写文件.py

import sys
import sy1_pb2 stuff1 = sy1_pb2.stuff() #stuff1.stuff_ID=1001
stuff1.stuff_ID=int(input("请输入商品编号:"))
#stuff1.stuff_Name='book'
stuff1.stuff_Name=input("请输入商品名称:")
#stuff1.stuff_Num=30
stuff1.stuff_Num=int(input("请输入库存:"))
#print(stuff) #反序列化
#stuff_unserial =stuff1.ParseFromString() #序列化
#stuff_serial = stuff1.SerializeToString() print('-------------') with open("sy1.txt",'wb')as f:
f.write(stuff1.SerializeToString())
print("添加成功!")

读文件.py

import sys
import sy1_pb2 stuff1 = sy1_pb2.stuff() with open("sy1.txt",'rb')as f:
stuff1.ParseFromString(f.read()) print(stuff1)
print("读取成功!")

怎么样,484很简单~

python之~ 序列化与反序列化的更多相关文章

  1. python的序列化和反序列化以及json

    python 的序列化和反序列化用于内存之间的共享,包括服务器和客户端的共享,两个Python程序之间的共享,以及以字符串的形式存储到硬盘中. pyhton 的pickle 可以对Python的各种数 ...

  2. python json序列化与反序列化操作

    python json序列化与反序列化操作 # dumps() dict-->str 序列化 # loads() str---dict 反序列化 result1 = json.dumps({'a ...

  3. python 的序列化和反序列化

    什么是序列化?简单来说就是将数据存储到物理内存上的过程叫序列化. 什么是反序列化?将数据从物理内存存储到程序内存的过程叫做反序列化. 下面来看一下python中使用json进行序列化和反序列化的实例d ...

  4. Python Json序列化与反序列化

    在python中,序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象.在python的标准库中,专门提供了json ...

  5. python 之 序列化与反序列化、os模块

    6.6 序列化与反序列化 特殊的字符串 , 只有:int / str / list / dict 最外层必须是列表或字典,如果包含字符串,必须是双引号"". 序列化:将Python ...

  6. python:序列化与反序列化(json、pickle、shelve)

    本节内容 前言 json模块 pickle模块 shelve模块 总结 一.前言 1. 现实需求 每种编程语言都有各自的数据类型,其中面向对象的编程语言还允许开发者自定义数据类型(如:自定义类),Py ...

  7. python的序列化与反序列化

    ------------------------------------------------------------------- 文件的序列化与反序列化:

  8. 【python】序列化和反序列化

    序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象.在python的标准库中,专门提供了json库与pickle库 ...

  9. Python的序列化和反序列化

    序列化是将dict---->str 反序列化是将str---->dict import jsonresult1 = json.dumps({'a': 1, 'b': 2}) #序列化res ...

随机推荐

  1. java类静态域、块,非静态域、块,构造函数的初始化顺序

    原文:http://ini.iteye.com/blog/2007835 面试的时候,经常会遇到这样的考题:给你两个类的代码,它们之间是继承的关系,每个类里只有构造器方法和一些变量, 构造器里可能还有 ...

  2. 虚拟机学习centos服务器版

    虚拟机安装下载教程:http://www.cnblogs.com/CyLee/p/5615322.html centos 6.5下载地址:http://www.centoscn.com/CentosS ...

  3. [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2

     Just like passing in an array to *ngFor, you can pass in any value into your structural directive s ...

  4. GridView”的控件 必须放在具有 runat=server 的窗体标记内 “错误提示”

    在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记 解决办法  重写 VerifyRendering ...

  5. OSI模型第四层传输层--TCP协议

    1.传输层2个协议tcp和udp 2.tcp的可靠性(挂号信). 面向链接的:类似寄挂号信,对方收到了并且能够确认.所以也是可靠的传输. 最大报文传输:两端可以协商传输报文大小.(协商一个报文的大小) ...

  6. Java 并发 中断线程

    Java 并发 中断线程 @author ixenos 对Runnable.run()方法的三种处置情况 1.在Runnable.run()方法的中间中断它 2.等待该方法到达对cancel标志的测试 ...

  7. C# CacheHelper

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  8. android获得屏幕高度和宽度(display中getSize(Point)方法使用)

    方法一: public static int SCREEN_WIDTH; public static int SCREEN_HEIGHT; //获取屏幕 WindowManager wm = (Win ...

  9. CodeForces 675E Trains and Statistic

    贪心,递推,线段树,$RMQ$. 假设我们记$ans[i]$是以$i$点为起点对答案的贡献,那么答案就是$\sum\limits_{i = 1}^n {ans[i]}$. $ans[i]$怎么计算呢? ...

  10. C#基础--属性

    定义一个Book类: namespace ConsoleTest { public class Book { private string _bookIsbn; private string _boo ...