|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 颖颖M杰杰 于 2021-9-3 16:30 编辑
爬取到相关信息,在用for语句写入excel的时候发现,只保存最后一条信息。大家看看是哪问题?
def save_to_excel(item):
wb = openpyxl.Workbook()
ws = wb.active
ws["A1"] = "系编号"
ws["B1"] = "学校编号"
ws["C1"] = "年级信息"
ws["D1"] = "年度班级信息"
ws["E1"] = "出生日期"
ws["F1"] = "辅导员姓名"
ws["G1"] = "身份信息"
for each in item:
ws.append(each)
wb.save("个人编号信息.xlsx")
def main():
item = get_itme(res)
#print(item)
save_to_excel(item)
if __name__ == '__main__':
main()
[['0004002190', '000017757', '管理系2020届3班', '20916363', 19610706, '陈光明', '1975******17']]
[['0004003263', '000017757', '机电系2020届2班', '20917362', 19610710, '黄玲玉', '1973******15']]
[['0004002082', '000017757', '管理系2020届1班', '20915361', 19620119, '李铭荣', '1980******10'], ['0004002083', '000017757', '管理系2020届2班', '20915362', 19620200, '程锋', '1979******16'],['0004002084', '000017757', '管理系2020届3班', '20915363', 19620201, '李铭波', '1979******19']]
[['004002894', '000017757', '英语系2020届1班', '20916461', 19640315, '胡蓉霞', '1981******19']]
import openpyxl
res = 'res.txt'
def get_itme(res):
lst = []
with open(res) as f:
text = f.read().replace(',',',')
lst2 = text.split('\n')
for i in lst2:
lst+=eval(i)
return lst
def save_to_excel(item):
wb = openpyxl.Workbook()
ws = wb.active
ws["A1"] = "系编号"
ws["B1"] = "学校编号"
ws["C1"] = "年级信息"
ws["D1"] = "年度班级信息"
ws["E1"] = "出生日期"
ws["F1"] = "辅导员姓名"
ws["G1"] = "身份信息"
for each in item:
ws.append(each)
wb.save("个人编号信息.xlsx")
def main():
item = get_itme(res)
#print(item)
save_to_excel(item)
if __name__ == '__main__':
main()
|
|