python实时数据流设计_Python读取实时数据流示例

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、读取流数据源

如果数据是来自一个连续的数据源,我们需要读取连续数据,接下来

我们介绍一个适用于许多真是场景的简单解决方案,然而它并不是通用的。

操作步骤:

在本节中我们将想你演示如何读取一个实时变化的文件,并把输入打印出来。

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读取实时数据流示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持云海天教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值