python之~ 序列化与反序列化

sy1.proto文件

syntax = "proto2";
message stuff
{
required int32 stuff_ID = 1;
required string stuff_Name = 2;
optional int32 stuff_Num = 3;

}

利用编译器转化成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很简单~

posted on 2016-08-11 15:30  Pheobeee  阅读(6971)  评论(0编辑  收藏  举报