python分页查询数据处理方法
def view_per_data(page_num, per_page_data):
'''
获取指定页码的数据
:param page_num: 页码
:param per_page_data: 每页数据
:return:
'''
page_num = int(page_num)
per_page_data = int(per_page_data)
view_data = []
view_data_count = 0
file_data = open('test.txt',mode='r',encoding='utf-8')
stat_index = (page_num - 1) * per_page_data
end_index = page_num * per_page_data
for line in file_data:
if stat_index <= view_data_count < end_index:
view_data.append(line.strip())
if view_data_count >= end_index:
break
view_data_count += 1
return view_data
def news_title(page_num, per_page_data):
'''
指定页码数据展示处理
:param page_num: 页码
:param per_page_data: 每页数据
:return:
'''
news_list = view_per_data(page_num, per_page_data)
index = (page_num - 1) * per_page_data + 1 #起始id
#处理数据展示指定数据内容和数据下标
for num, line in enumerate(news_list, index):
data = line.strip().split(',')[1]
print(f'{num} {data}')
def execute_view():
'''
执行指定页码数据查看操作
:return:
'''
per_page_data = 10
page_count = 999
# 计算页码最大值
max_page_num, remainder = divmod(page_count, per_page_data)
if remainder:
max_page_num += 1
while True:
num = input(f'请输入页码,页码范围{0}~{max_page_num}:').strip()
if num.isdigit():
num = int(num)
if num > max_page_num or num < 1:
continue
news_title(num, per_page_data)
if num == 'Q':
break