Python讀取實(shí)時(shí)數(shù)據(jù)流示例
1、#coding:utf-8
chose = [ ('foo',1,2), ('bar','hello'), ('foo',3,4) ] def do_foo(x,y): print('foo',x,y) def do_bar(s): print('bar',s) for tag,*args in chose: if tag == 'foo': do_foo(*args) elif tag == 'bar': do_bar(*args) line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false' uname,*fields,homedir,sh = line.split(':') print(sh) from collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen=history) for li in lines: if pattern in li: yield li, previous_lines previous_lines.append(li) # Example use on a file if __name__ == '__main__': with open(r'./somefiles.py') as f: for line, prevlines in search(f, 'python', 5): for pline in prevlines: print(pline, end='') print(line, end='') print('-' * 20)
2、import heapq
portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price']) print(cheap) print(expensive)
3、讀取流數(shù)據(jù)源
如果數(shù)據(jù)是來自一個(gè)連續(xù)的數(shù)據(jù)源,我們需要讀取連續(xù)數(shù)據(jù),接下來
我們介紹一個(gè)適用于許多真是場景的簡單解決方案,然而它并不是通用的。
操作步驟:
在本節(jié)中我們將想你演示如何讀取一個(gè)實(shí)時(shí)變化的文件,并把輸入打印出來。
import time import os import sys if len(sys.argv) != 2: print('>>sys.stderr,"請輸入需要讀取的文件名!"') filename = sys.argv[1] if not os.path.isfile(filename): print('>>sys.stderr,"請給出需要的文件:\%s\: is not a file" % filename') with open(filename,'r') as f: filesize = os.stat(filename)[6] f.seek(filesize) while True: where = f.tell() line = f.readline() if not line: time.sleep(1) f.seek(where) else: print(line)
以上這篇Python讀取實(shí)時(shí)數(shù)據(jù)流示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Python實(shí)現(xiàn)的堆排序算法原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的堆排序算法,簡單描述了堆排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)堆排序的相關(guān)操作技巧,代碼中備有較為詳細(xì)的注釋便于理解,需要的朋友可以參考下2017-11-11pandas探索你的數(shù)據(jù)實(shí)現(xiàn)可視化示例詳解
這篇文章主要為大家介紹了pandas探索你的數(shù)據(jù)實(shí)現(xiàn)可視化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Mac上Python使用ffmpeg完美解決方案(避坑必看!)
ffmpeg是一個(gè)強(qiáng)大的開源命令行多媒體處理工具,下面這篇文章主要給大家介紹了關(guān)于Mac上Python使用ffmpeg完美解決方案的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02對Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解
今天小編就為大家分享一篇對Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python?Flask實(shí)現(xiàn)快速構(gòu)建Web應(yīng)用的方法詳解
Flask是一個(gè)輕量級的Web服務(wù)器網(wǎng)關(guān)接口(WSGI)web應(yīng)用框架,本文將和大家一起詳細(xì)探討一下Python?Flask?Web服務(wù),需要的小伙伴可以學(xué)習(xí)一下2023-06-06python線程池的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了python線程池的實(shí)現(xiàn)方法,代碼簡單實(shí)用,供大家參考使用2013-11-11