zoukankan      html  css  js  c++  java
  • python文件操作seek()偏移量,读取指正到指定位置操作

    file.seek()方法格式: seek(offset,whence=0) 移动文件读取指针到制定位置
    offset:开始的偏移量,也就是代表需要移动偏移的字节数。
    whence: 给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头算起,1代表开始从当前位置开始算起,2代表从文件末尾开始算起。当有换行时,会被换行截断。  seek()无返回值,故值为None
    tell() : 文科文件的当前位置,即tell是获取文件指针位置。
    readline(n):读入若干行,n代表读入的最长字节数。
    readlines() :读入所有行的内容
    read读入所有行的内容
    tell() : 返回文件读取指针的位置
    补充知识:python中limit()和offset()的用法
    limit()限制结果集每次值查询几条数据 offset()可以限制查找对象数据的时候过滤掉多少条切片,可以对Query对象使用切片操作,来获取想要的数据,可以使用 select(start,stop)方法来求片操作,也可以使用'[start:stop]的方式来进行切片操作,
    在实际开发中,中括号形式的是用处较多的,希望大家掌握
    #encoding: utf-8
     
    from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,
    DateTime
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker
    from random import randint
    from datetime import datetime
     
    HOSTNAME = '127.0.0.1'
    PORT = 3306
    DATABASE = 'first_sqlalchemy'
    USERNAME = 'root'
    PASSWORD = '123456'
     
    DB_URI = "mysql+pymysql://{username}:{password}@{host}:{port}/"
    "{db}?charset=utf8".format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)
     
    engine = create_engine(DB_URI)
    Base = declarative_base(engine)
     
    # Session = sessionmaker(engine)
    # session = Session()
    session = sessionmaker(engine)() #Session(**local_kw)
     
    class Article(Base):
    __tablename__ = 'article'
    id = Column(Integer,primary_key=True,autoincrement=True)
    title = Column(String(50),nullable=False)
    create_time = Column(DateTime,default=datetime.now)
     
    def __repr__(self):
    return '<article:{title}>'.format(title=self.title)
     
    # Base.metadata.drop_all()
    #
    # Base.metadata.create_all()
    #
    #
    # for x in range(0,100):
    # article = Article(title = 'title%s'%x)
    # session.add(article)
    # session.commit()
     
    #第一limit的用法,限制查询多少数据
    article = session.query(Article).limit(10).all()#用limit限制只查询10个数据
    print(article)
     
    #第二个参数offset的用法,本意是偏移量,在这里就是从多少开始查询
    article_offset = session.query(Article).offset(10).all()
    print(article_offset)
     
    #offset和limit联合起来用,就相当于python 的字符串和列表、元祖的切片操作
    article_offset_limit = session.query(Article).offset(10).limit(5).all()
    print(article_offset_limit)
     
    #如果查询最新的10篇文章,就可以用order_by 和 limit 一起用
    article_order_by_limit = session.query(Article).order_by(Article.id.desc()).limit(10).all()
    print(article_order_by_limit)
     
    #slice,本身就是切片的意思
    article_order_by_slice = session.query(Article).order_by(Article.id.desc()).slice(0,10).all()
    print(article_order_by_slice)
     
    #还有一个更简单的方法,就想python的列表切片操作
     
    article_list_slice = session.query(Article).order_by(Article.id.desc())[0:10]
    print(article_list_slice)
     
    以上这篇python文件操作seek()偏移量,读取指正到指定位置操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
    每日分享,喜欢的看标题和多多点赞收藏加关注~~蟹蟹
  • 相关阅读:
    104.Maximum Depth of Binary Tree
    103.Binary Tree Zigzag Level Order Traversal
    102.Binary Tree Level Order Traversal
    101.Symmetric Tree
    100.Same Tree
    99.Recover Binary Search Tree
    98.Validate Binary Search Tree
    97.Interleaving String
    static静态初始化块
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/nanhe/p/13697888.html
Copyright © 2011-2022 走看看