1
我正在使用hadoop和python制作一个倒排索引。 我想知道如何在python中包含一行/字的字节偏移量。 我需要的是这样的如何获取python文件中的字节偏移量
hello [email protected]
我需要做一个完整的倒排索引的位置。 请帮忙。
我正在使用hadoop和python制作一个倒排索引。 我想知道如何在python中包含一行/字的字节偏移量。 我需要的是这样的如何获取python文件中的字节偏移量
hello [email protected]
我需要做一个完整的倒排索引的位置。 请帮忙。
是否这样?
file.tell()
返回文件的当前位置,如stdio的ftell()。
http://docs.python.org/library/stdtypes.html#file-objects
不幸的是告诉(),因为OP是使用标准输入,而不是一个文件不起作用。但为了给你所需要的东西打造一个包装并不难。
class file_with_pos(object):
def __init__(self, fp):
self.fp = fp
self.pos = 0
def read(self, *args):
data = self.fp.read(*args)
self.pos += len(data)
return data
def tell(self):
return self.pos
然后你就可以使用它代替:
fp = file_with_pos(sys.stdin)
我从sys.stdin和file.tell()读取输入似乎并没有在与它的工作.. – easysid 2010-09-07 18:03:33
添加包装类回答。 – 2010-09-07 18:22:06
谢谢你的回应...会尝试一下...但是,目前我已经实施了一个计数器变量来保持位置的跟踪。它工作得很好,因为我只需要文件中的相对位置。 – easysid 2010-09-09 20:20:15