Python读取实时数据流示例

作者:Python之魂 时间:2023-09-11 14:20:00 

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)

来源:https://blog.csdn.net/weixin_43431189/article/details/89341441

标签:Python,读取,数据流
0
投稿

猜你喜欢

  • Python特效之数字成像方法详解

    2022-09-12 13:16:22
  • js自动闭合html标签(自动补全html标记)

    2023-08-25 07:06:35
  • 能说明你的Javascript技术很烂的五个原因分析

    2024-04-29 14:07:36
  • 解析zend studio中直接导入svn中的项目的方法步骤

    2023-09-05 02:21:01
  • Python Pivot table透视表使用方法解析

    2021-06-21 10:22:59
  • PyTorch中view()与 reshape()的区别详析

    2023-11-16 05:45:23
  • 随机6+1选号码摇奖程序

    2008-07-18 13:15:00
  • 教你使用Python提取视频中的美女图片

    2021-05-21 22:41:50
  • python urllib库的使用详解

    2021-06-12 14:42:04
  • YOLOv5改进之添加CBAM注意力机制的方法

    2023-07-22 20:48:52
  • sqlserver 千万数量级分页存储过程代码

    2024-01-14 07:50:13
  • python使用pymongo与MongoDB基本交互操作示例

    2023-11-27 03:33:19
  • python获得一个月有多少天的方法

    2022-02-21 00:55:15
  • js里面的变量范围分享

    2024-04-19 09:51:47
  • Hibernate 的原理与配置

    2023-07-20 21:26:52
  • go语言数据结构之前缀树Trie

    2023-08-05 18:15:50
  • 表单验证通用脚本(支持所有主流浏览器)

    2010-08-08 08:54:00
  • Python NumPy灰度图像的压缩原理讲解

    2021-02-16 02:46:33
  • SQL语句练习实例之七 剔除不需要的记录行

    2024-01-17 21:15:04
  • Go语言开发编程规范命令风格代码格式

    2024-05-22 10:15:03
  • asp之家 网络编程 m.aspxhome.com