2016-04-25 91 views
-2

林通過SFTP拉在一個文件中,並且無法計數出現打開一個文件並計算出現'|||'的次數

try: 
     with open(os.path.join(args.local, file)) as rcount: 
      count= rcount.read().strip().split('|||') 
      #count = list(rcount)[-1].rstrip().split('|||')[1] 
      logging.info('Number of count: %d' % int(count)) 
      name = match('([A-Z0-9]+)', file) 

也不指望語句的工作數量。

在文件中將會多次出現 YYYYMMDD | NNNNNNNNXXXXXX | Accepted |||

我的邏輯是計算文件中出現[|||]的次數。我還可以閱讀「接受」一詞。

+4

'file.read()。count('|||')'? – timgeb

回答

1

由於@timgeb曾評論,你可以使用一個字符串的方法count(),例如: -

with open(os.path.join(args.local, file)) as rcount: 
    count = rcount.read().count("|||") 
    logging.info('Number of count: %d' % int(count)) 

希望這有助於!

+0

警告2016-04-25 09:01:50.533:'str'對象沒有屬性'讀' – Steve

+0

@Steve調用文件對象上的'read' ... – timgeb

+0

道歉,這是我的錯誤。這似乎工作,但得到不尋常的結果。進一步看。謝謝 – Steve