str_len = struct.unpack(' q',len_bytes)[0]

时间:2018-01-29 03:47:29

标签: python nlp deep-learning

我正在学习文字摘要。你能告诉我这段代码的含义吗?

with open(f, 'rb') as reader:
          while True:
            len_bytes = reader.read(8)
            if not len_bytes: break
            str_len = struct.unpack('q', len_bytes)[0]
            example_str = struct.unpack('%ds' % str_len, reader.read(str_len))[0]
            e = example_pb2.Example.FromString(example_str)

1 个答案:

答案 0 :(得分:-1)

注意:问题已在您尝试学习时得到解答。理想情况下,这个问题应该关闭并标记为“非主题”!

据我所知,这是来自tensorflow文本摘要示例。您可能需要查看代码以获得更好的解释。

二进制文件包含格式的数据。 str_len提取零件并确定斑点的长度。 example_str长度为str_len,接下来会被提取。

with open(f, 'rb') as reader:                  # opens the file in binary mode.
          while True:
            len_bytes = reader.read(8)         # reads 8 characters
            if not len_bytes: break            # keep reading the file till EOF
            str_len = struct.unpack('q', len_bytes)[0]    #Unpack the data as an 'integer'(due to format 'q'). This is the length of the string
            example_str = struct.unpack('%ds' % str_len, reader.read(str_len))[0] # Unpacks the data as a 'string' of length 'str_len'
            e = example_pb2.Example.FromString(example_str)