2017-04-07 48 views
0

我应该保持一个偏移量文件并读取该偏移线,放出,更新偏移=偏移+ 1需要字符串或缓冲区,文件中发现

class SimSpout(storm.Spout): 

    # Not much to do here for such a basic spout 
    def initialize(self, conf, context): 
    ## Open the file with read only permit 
    self.f = open('data.txt', 'r') 
    ## Read the first line 
    self._conf = conf 
    self._context = context 
    self._offset = 0 
    storm.logInfo("Spout instance starting...") 

# Process the next tuple 
def nextTuple(self): 
    # check if it reach at the EOF to close it 
    with open(self.f) as f: 
     f.readlines()[self._offset] 
     #Emit a random sentence 
     storm.logInfo("Emiting %s" % line) 
     storm.emit([line]) 
    self._offset = self._offset + 1 

,但我得到的错误

with open(self.f) as f: 
TypeError: coercing to Unicode: need string or buffer, file found 

回答

0

您正在打开此行中的文件

self.f = open('data.txt', 'r') 

并试图打开文件句柄而不是此行中的同一文件

with open(self.f) as f: 

相反,在nextTuple,只是使用的self.f代替f

+0

感谢回答,但对不起,我是新来的蟒蛇。你的意思是用open打开(self.f): self.f.readlines()[self._offset]? – user3188912

+0

你还在吗? – user3188912

相关问题