本文整理匯總了Python中pickle.read方法的典型用法代碼示例。如果您正苦於以下問題:Python pickle.read方法的具體用法?Python pickle.read怎麽用?Python pickle.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pickle
的用法示例。
在下文中一共展示了pickle.read方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: read_string4
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_string4(f):
r"""
>>> import StringIO
>>> read_string4(StringIO.StringIO("\x00\x00\x00\x00abc"))
''
>>> read_string4(StringIO.StringIO("\x03\x00\x00\x00abcdef"))
'abc'
>>> read_string4(StringIO.StringIO("\x00\x00\x00\x03abcdef"))
Traceback (most recent call last):
...
ValueError: expected 50331648 bytes in a string4, but only 6 remain
"""
n = read_int4(f)
if n < 0:
raise ValueError("string4 byte count < 0: %d" % n)
data = f.read(n)
if len(data) == n:
return data
raise ValueError("expected %d bytes in a string4, but only %d remain" %
(n, len(data)))
示例2: read_long1
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_long1(f):
r"""
>>> import StringIO
>>> read_long1(StringIO.StringIO("\x00"))
0L
>>> read_long1(StringIO.StringIO("\x02\xff\x00"))
255L
>>> read_long1(StringIO.StringIO("\x02\xff\x7f"))
32767L
>>> read_long1(StringIO.StringIO("\x02\x00\xff"))
-256L
>>> read_long1(StringIO.StringIO("\x02\x00\x80"))
-32768L
"""
n = read_uint1(f)
data = f.read(n)
if len(data) != n:
raise ValueError("not enough data in stream to read long1")
return decode_long(data)
示例3: read_long4
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_long4(f):
r"""
>>> import StringIO
>>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\xff\x00"))
255L
>>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\xff\x7f"))
32767L
>>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\xff"))
-256L
>>> read_long4(StringIO.StringIO("\x02\x00\x00\x00\x00\x80"))
-32768L
>>> read_long1(StringIO.StringIO("\x00\x00\x00\x00"))
0L
"""
n = read_int4(f)
if n < 0:
raise ValueError("long4 byte count < 0: %d" % n)
data = f.read(n)
if len(data) != n:
raise ValueError("not enough data in stream to read long4")
return decode_long(data)
示例4: read_uint1
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_uint1(f):
r"""
>>> import StringIO
>>> read_uint1(StringIO.StringIO('\xff'))
255
"""
data = f.read(1)
if data:
return ord(data)
raise ValueError("not enough data in stream to read uint1")
示例5: read_uint2
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_uint2(f):
r"""
>>> import StringIO
>>> read_uint2(StringIO.StringIO('\xff\x00'))
255
>>> read_uint2(StringIO.StringIO('\xff\xff'))
65535
"""
data = f.read(2)
if len(data) == 2:
return _unpack("<H", data)[0]
raise ValueError("not enough data in stream to read uint2")
示例6: read_int4
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_int4(f):
r"""
>>> import StringIO
>>> read_int4(StringIO.StringIO('\xff\x00\x00\x00'))
255
>>> read_int4(StringIO.StringIO('\x00\x00\x00\x80')) == -(2**31)
True
"""
data = f.read(4)
if len(data) == 4:
return _unpack("<i", data)[0]
raise ValueError("not enough data in stream to read int4")
示例7: read_unicodestringnl
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_unicodestringnl(f):
r"""
>>> import StringIO
>>> read_unicodestringnl(StringIO.StringIO("abc\uabcd\njunk"))
u'abc\uabcd'
"""
data = f.readline()
if not data.endswith('\n'):
raise ValueError("no newline found when trying to read "
"unicodestringnl")
data = data[:-1] # lose the newline
return unicode(data, 'raw-unicode-escape')
示例8: read_unicodestring4
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_unicodestring4(f):
r"""
# bug 24549
#>>> import StringIO
#>>> s = u'abcd\uabcd'
#>>> enc = s.encode('utf-8')
#>>> enc
#'abcd\xea\xaf\x8d'
#>>> n = chr(len(enc)) + chr(0) * 3 # little-endian 4-byte length
#>>> t = read_unicodestring4(StringIO.StringIO(n + enc + 'junk'))
#>>> s == t
#True
#
#>>> read_unicodestring4(StringIO.StringIO(n + enc[:-1]))
#Traceback (most recent call last):
#...
#ValueError: expected 7 bytes in a unicodestring4, but only 6 remain
"""
n = read_int4(f)
if n < 0:
raise ValueError("unicodestring4 byte count < 0: %d" % n)
data = f.read(n)
if len(data) == n:
return unicode(data, 'utf-8')
raise ValueError("expected %d bytes in a unicodestring4, but only %d "
"remain" % (n, len(data)))
示例9: read_float8
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import read [as 別名]
def read_float8(f):
r"""
#>>> import StringIO, struct
#>>> raw = struct.pack(">d", -1.25)
#>>> raw
#'\xbf\xf4\x00\x00\x00\x00\x00\x00'
#>>> read_float8(StringIO.StringIO(raw + "\n"))
#-1.25
"""
data = f.read(8)
if len(data) == 8:
return _unpack(">d", data)[0]
raise ValueError("not enough data in stream to read float8")