Python 读取 Excel 中符合特定条件的数据,并写入新的表格

2022-06-15 09:30:22 浏览数 (1)

原始表格

代码

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/3/20 21:24
# @Author  : cunyu
# @Site    : cunyu1943.github.io
# @File    : LimitedInfo.py
# @Software: PyCharm

import xlrd
import xlwt

file = '网易新闻.xls'
data = xlrd.open_workbook(file)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols

print(nrows, ncols)

workbook = xlwt.Workbook(encoding='utf-8')
news_sheet = workbook.add_sheet('news')
news_sheet.write(0, 0, 'Title')
news_sheet.write(0, 1, 'Content')
data = input('Input the Date,format(2019-03-19:)n')

rank_list = []
for i in range(1, nrows):
	if table.row_values(i)[-1] == data:
		print(table.row_values(i)[-1])
		print(i)
		rank_list.append(i)
print(rank_list)

for i in range(len(rank_list)):
	news_sheet.write(i 1, 0, table.row_values(int(rank_list[i]))[0])
	news_sheet.write(i 1, 1, table.row_values(int(rank_list[i]))[1])

workbook.save('%s-网易新闻.xls' %(data))

写入符合条件数据后新的表格

0 人点赞