SoFunction
Updated on 2024-11-16

python file manipulation seek offset, read correction to the specified position operation

python file operations seek() and telll() self-explanatory

() method format: seek(offset,whence=0) move the file read pointer to the specified position

offset:The start offset, that is, the number of bytes representing the offset to be moved.

whence: gives a definition to the offset parameter, indicating the position to start offsetting from; 0 means counting from the beginning of the file, 1 means starting counting from the current position, 2 means counting from the end of the file. When there is a line break, it is truncated by the line break.  Seek() has no return value, so the value is None.

tell() : The current location of the text file, i.e. tell is to get the file pointer location.

readline(n): read a number of lines, n represents the longest number of bytes read.

readlines() :reads all lines.

read reads the contents of all lines

tell() : Returns the location of the file read pointer.

Additional knowledge:Usage of limit() and offset() in python

limit () to limit the result set of each value query a few data offset () can limit the search object data when filtering out how many slices, you can use the Query object slicing operations, to get the data you want to use select (start, stop) method to find the slice of the operation, but also can use '[start: stop] way to slicing operations.

In the actual development, the form of parentheses is more useful, I hope you grasp the

#encoding: utf-8

from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,\
  DateTime
from  import declarative_base
from  import sessionmaker
from random import randint
from datetime import datetime

HOSTNAME = '127.0.0.1'
PORT = 3306
DATABASE = 'first_sqlalchemy'
USERNAME = 'root'
PASSWORD = '123456'

#dialect+driver://username:password@host:port/database
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=)

  def __repr__(self):
    return '<article:{title}>'.format(title=)

# .drop_all()
#
# .create_all()
#
#
# for x in range(0,100):
#   article = Article(title = 'title%s'%x)
#   (article)
# ()

# First limit usage, limit how much data to query
article = (Article).limit(10).all()# Limit the query to only 10 data with limit
print(article)

# The second parameter offset is used, which is meant to be an offset, in this case it's a query starting from how many
article_offset = (Article).offset(10).all()
print(article_offset)

The combination of #offset and #limit is the python equivalent of slicing strings, lists, and ancestors.
article_offset_limit = (Article).offset(10).limit(5).all()
print(article_offset_limit)

#If you want to query the latest 10 articles, you can use order_by and limit together.
article_order_by_limit = (Article).order_by(()).limit(10).all()
print(article_order_by_limit)

#Slice, itself, means slice #
article_order_by_slice = (Article).order_by(()).slice(0,10).all()
print(article_order_by_slice)

# There's a simpler way to do this, think python list slicing operations

article_list_slice = (Article).order_by(())[0:10]
print(article_list_slice)

Above this python file operation seek() offset, read pointing to the specified location operation is all I have shared with you, I hope to be able to give you a reference, and I hope you support me more.