Python读取Excel中符合特定条件的数据,并写入新的表格中
程序员文章站
2022-04-30 18:25:35
...
原始表格
代码
#!/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))