当前位置:   article > 正文

Pyhton将数据存入Execel、csv中_python 接口数据保存到excel

python 接口数据保存到excel

今日在练习python的时候想着也练练将数据存入execel表格中,在网上找了许多资料没有自己想要的demo,非常难受。故记录一下!

数据格式是json,这个就不多讲了。

一、写入Excel

使用 openpyxl 库

  • 安装库:pip install openpyxl
  • 导入库:from openpyxl import Workbook

开胃菜,先来一个简单的demo

将json数据存入execel中,设置第一行为标题,依次存入数据

# 导入库
from openpyxl import Workbook

# 创建 Workbook 工作表
wb = Workbook()
# 设置当前活动工作表
ws = wb.active

# 设置行标题
row_title = ['MovieRank','MovieID','MovieName','BoxOffice','SumBoxOffice','AvgPrice','AvpPeoPle','MovieDay','DefaultImage','Rank_Up','BoxOffice_Up','RapIndex']
ws.append(row_title)

# 遍历数据,
for i,item in enumerate(json.loads(datas)['Data']['Table']):
    # 使用列表推导式:遍历行标题,将表格对应的单元格数据放入数组中,如果没有设置为 ''
    row_data = [item.get(row,'') for row in row_title]
    # 写入工作簿
    ws.append(row_data)

# 保存工作簿
wb.save('data.xlsx')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

打开excel查看数据如下:
在这里插入图片描述
ok,现在已经基本搞定了,只是行标题好像有点不明显,继续往下看,行标题加粗。

补充

这里提一嘴关于 row_data = [item.get(row,'') for row in row_title] 写法的解释:

  • 它使用了列表推导式,将每个表头对应的单元格数据放入一个列表中。
  • 具体来说,row_title是一个包含所有表头的列表,item是一个字典,包含了当前行的所有数据。item.get(row, '')的作用是从字典row中获取键为row的值,如果该键不存在,则返回空字符串。这样,row_data列表中的每个元素就对应了当前行中每个表头所对应的单元格数据。

行标题加粗

# 导入Font类
from openpyxl.styles import Font

# 创建 Workbook 工作表
wb = Workbook()
# 设置当前活动工作表
ws = wb.active

# 设置行标题
row_title = ['MovieRank','MovieID','MovieName','BoxOffice','SumBoxOffice','AvgPrice','AvpPeoPle','MovieDay','DefaultImage','Rank_Up','BoxOffice_Up','RapIndex']

# 使用 enumerate() 函数将一个可迭代对象转换为一个枚举对象,并设置起始索引为1
for col_num,title in enumerate(row_title,1):
    # 设置单元格的值
    # row和colum分别的单元格的行号和列号。value是单元格的内容
    cell = ws.cell(row=1,column=col_num,value=title)
    # 设置单元格字体样式:加粗
    cell.font = Font(bold=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

效果如下:
在这里插入图片描述

进阶字体设置

# 一次性设置字体名称、字体加粗、字体大小、字体颜色
font = Font(name='楷体',bold=True,size=18,color='b6bee2')
cell.font = font
  • 1
  • 2
  • 3

直观的看下代码:
在这里插入图片描述
效果展示:
在这里插入图片描述

单元格填充样式

# 导入Font、PatternFill类
from openpyxl.styles import Font,PatternFill

# 设置单元格填充样式,设置背景颜色
fill = PatternFill(fill_type='solid',start_color='596dc8')
cell.fill = fill
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下:
在这里插入图片描述

补充

  • PatternFill 是 openpyxl.styles 模块中的一个类,用于设置单元格的填充样式。您可以使用 PatternFill 类中的属性来设置单元格的填充颜色、填充样式等。以下是一些常用的属性:
    • fill_type:填充类型,可以是 solid(纯色填充)或 pattern(图案填充)。
    • start_color:填充起始颜色。
    • end_color:填充结束颜色。
    • patternType:图案填充类型,可以是 solid、darkDown、darkGray、darkGrid、darkHorizontal、 darkTrellis、darkUp、darkVertical、gray0625、gray125、lightDown、lightGray、lightGrid、lightHorizontal、lightTrellis、lightUp、lightVertical、mediumGray 等。

具体使用可以自行查询资料,这里我没有过多的使用所以及不做太多记录。

设置单元格对齐方式

# 导入Font、PatternFill、Alignment类
from openpyxl.styles import Font,PatternFill,Alignment

# 设置单元格对齐方式:水平居中、垂直居中
alignment = Alignment(horizontal='center',vertical='center')
cell.alignment = alignment
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下:
在这里插入图片描述

补充

  • alignment 是 openpyxl 中用于设置单元格对齐方式的类。除了设置水平和垂直对齐方式外,alignment 还提供了其他一些有用的属性
    • horizontal 参数指定了水平对齐方式,可以设置为 ‘center’(居中对齐)、‘left’(左对齐)或 ‘right’(右对齐)
    • indent=1 设置单元格文本的缩进
    • wrap_text=True 设置单元格文本自动换行
    • textRotation=45 逆时针旋转45度
    • vertical='center' 垂直居中

更多使用自行探索哦。

代码

下面代码中不包含我抓取网页数据的代码哦!不过我会把datas的数据放在下面,需要的自行解析哦.

# datas数据 “需要解析”
datas = {"Status":1,"Msg":"","Data":{"Table":[{"MovieRank":1,"MovieID":695734,"MovieName":"坚如磐石","BoxOffice":12493,"SumBoxOffice":99979,"AvgPrice":43,"AvpPeoPle":10,"MovieDay":11,"DefaultImage":"https://images.entgroup.cn/group2/M00/1E/4A/wKgAS2TJ1W-Ad4WZAABVecIUS3k607.jpg","Rank_Up":9999,"BoxOffice_Up":-69,"RapIndex":8.33},{"MovieRank":2,"MovieID":706186,"MovieName":"前任4:英年早婚","BoxOffice":9556,"SumBoxOffice":76304,"AvgPrice":42,"AvpPeoPle":10,"MovieDay":11,"DefaultImage":"https://images.entgroup.cn/group2/M00/1E/9B/wKgAS2TbJ6eAPUfAAABtMveLuzM431.jpg","Rank_Up":9999,"BoxOffice_Up":-68,"RapIndex":8.27},{"MovieRank":3,"MovieID":661351,"MovieName":"莫斯科行动","BoxOffice":7011,"SumBoxOffice":44547,"AvgPrice":43,"AvpPeoPle":9,"MovieDay":10,"DefaultImage":"https://images.entgroup.cn/group2/M00/1F/07/wKgAS2TwbSSAJqapAABhWFkOmMY48.jpeg","Rank_Up":1,"BoxOffice_Up":-58,"RapIndex":8.46},{"MovieRank":4,"MovieID":707875,"MovieName":"志愿军:雄兵出击","BoxOffice":6766,"SumBoxOffice":55808,"AvgPrice":41,"AvpPeoPle":8,"MovieDay":11,"DefaultImage":"https://images.entgroup.cn/group2/M00/1F/19/wKgAS2T1UBOAQ6ZKAABrfZWP5kk67.jpeg","Rank_Up":-1,"BoxOffice_Up":-70,"RapIndex":8.88},{"MovieRank":5,"MovieID":713130,"MovieName":"好像也没那么热血沸腾","BoxOffice":1699,"SumBoxOffice":12661,"AvgPrice":42,"AvpPeoPle":6,"MovieDay":11,"DefaultImage":"https://images.entgroup.cn/group2/M00/1D/A2/wKgAS2SxKCSAGhzjAAB_yNWH0W0066.jpg","Rank_Up":1,"BoxOffice_Up":-56,"RapIndex":8.76},{"MovieRank":6,"MovieID":741050,"MovieName":"汪汪队立大功大电影2:超能大冒险","BoxOffice":1155,"SumBoxOffice":9673,"AvgPrice":33,"AvpPeoPle":7,"MovieDay":10,"DefaultImage":"https://images.entgroup.cn/group2/M00/1F/35/wKgASmT_1GWAY-pBAACF8dyrzn072.jpeg","Rank_Up":-1,"BoxOffice_Up":-70,"RapIndex":9.02},{"MovieRank":7,"MovieID":709619,"MovieName":"贝肯熊:火星任务","BoxOffice":619,"SumBoxOffice":6044,"AvgPrice":37,"AvpPeoPle":5,"MovieDay":11,"DefaultImage":null,"Rank_Up":9999,"BoxOffice_Up":-73,"RapIndex":9.10},{"MovieRank":8,"MovieID":705157,"MovieName":"潜艇总动员:环游地球80天","BoxOffice":274,"SumBoxOffice":3749,"AvgPrice":65,"AvpPeoPle":84,"MovieDay":58,"DefaultImage":"https://images.entgroup.cn/group1/M00/3B/22/wKgASWSudzqAH5OcAACFr8TGtxg613.jpg","Rank_Up":2,"BoxOffice_Up":32,"RapIndex":7.60},{"MovieRank":9,"MovieID":737593,"MovieName":"奥本海默","BoxOffice":239,"SumBoxOffice":44050,"AvgPrice":49,"AvpPeoPle":9,"MovieDay":40,"DefaultImage":"https://images.entgroup.cn/group2/M00/1E/4A/wKgAS2TJ_suAa8rOAAB42s5VT-034.jpeg","Rank_Up":9999,"BoxOffice_Up":-54,"RapIndex":9.25},{"MovieRank":10,"MovieID":697514,"MovieName":"我是哪吒2之英雄归来","BoxOffice":135,"SumBoxOffice":1478,"AvgPrice":37,"AvpPeoPle":3,"MovieDay":10,"DefaultImage":"https://images.entgroup.cn/group2/M00/1D/A2/wKgAS2SvlPmACQiqAAB4BHMJNgM092.jpg","Rank_Up":-2,"BoxOffice_Up":-80,"RapIndex":null}],"Table1":[{"sDate":"2023-10-06至2023-10-08","BoxOffice":40544,"ShowCount":1138038,"AudienceCount":968}]}}

  • 1
  • 2
  • 3
import json
# 导入库
from openpyxl import Workbook
# 导入Font、PatternFill、Alignment类
from openpyxl.styles import Font,PatternFill,Alignment

# 创建 Workbook 工作表
wb = Workbook()
# 设置当前活动工作表
ws = wb.active

# 设置行标题
row_title = ['MovieRank','MovieID','MovieName','BoxOffice','SumBoxOffice','AvgPrice','AvpPeoPle','MovieDay','DefaultImage','Rank_Up','BoxOffice_Up','RapIndex']
# 使用 enumerate() 函数将一个可迭代对象转换为一个枚举对象,并设置起始索引为1
for col_num,title in enumerate(row_title,1):
    # 设置单元格的值
    # row和colum分别的单元格的行号和列号。value是单元格的内容
    cell = ws.cell(row=1,column=col_num,value=title)
    # 一次性设置字体名称、字体加粗、字体大小、字体颜色
    font = Font(name='楷体',bold=True,size=18,color='ffffff')
    cell.font = font

    # 设置单元格填充样式
    fill = PatternFill(fill_type='solid',start_color='596dc8')
    cell.fill = fill

    # 设置单元格对齐方式:水平居中、垂直居中
    alignment = Alignment(horizontal='center',vertical='center')
    cell.alignment = alignment
    #indent=10,wrap_text=True,textRotation=45
# 遍历数据,
for i,item in enumerate(json.loads(datas)['Data']['Table']):
    # 使用列表推导式:遍历行标题,将表格对应的单元格数据放入数组中,如果没有设置为 ''
    row_data = [item.get(row,'') for row in row_title]
    # 写入工作簿
    ws.append(row_data)

# 保存工作簿
wb.save('data.xlsx')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

二、写入 csv

更新日期:2023/11/8

今日在抓取数据的时候想到不能一直写入excel吧,试试其他的写入。这不了解到了csv。下面看看吧。

实现代码

我先把实现代码贴出来。后面再记录实现的原理。
在这里插入图片描述

import csv

# 写入 csv 文件
with open("data.csv",'w',newline='',encoding='utf-8') as csvfile:
    # 创建 csv 写入器
    writer = csv.writer(csvfile)
    # 写入标题行
    writer.writerow(row_title)
    # 写入行数据
    for index,item in enumerate(result_data['data']['rows']):
        writer.writerow([item.get(title,'') for title in row_title])
    print('文件写入成功')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 数据
result_data = "{'code': 0, 'msg': '操作成功', 'data': {'total': 128, 'rows': [{'id': 549, 'release_name': 'SCTF 2023', 'release_sponsor': 'XCTF联赛', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-06-17 09:00:00', 'competition_end_time': '2023-06-19 09:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/6a0d3235-381e-4f81-a86c-d94c925491d0.jpg', 'is_comment_open': 0, 'is_official': 1, 'hash': '1107d986-0029-11ee-ab28-000c29bc20bf', 'competition_url': '', 'create_time': '2023-06-01 11:04:47', 'inside': 1, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': {'event_hash': 'c6285de0-0029-11ee-ab28-000c29bc20bf', 'status': 1}, 'event_type': 1, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 1}, 'is_test_event': 0, 'order_start_time': '2023-06-17 09:00:00', 'order_end_time': '2023-06-19 09:00:00'}, {'id': 546, 'release_name': '第十六届全国大学生信息安全竞赛—创新实践能力赛(初赛)', 'release_sponsor': '-', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-05-27 00:00:00', 'competition_end_time': '2023-05-28 23:00:00', 'sign_up_start_time': '2023-04-27 00:00:00', 'sign_up_end_time': '2023-05-26 00:00:00', 'release_cover': '/media/picture/cover/d08841d9-f09a-406e-b088-00f6609a6cb0.png', 'is_comment_open': 0, 'is_official': 2, 'hash': '2b460a86-ebf0-11ed-b5da-000c296d9df6', 'competition_url': None, 'create_time': '2023-05-06 17:27:07', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-05-27 00:00:00', 'order_end_time': '2023-05-28 23:00:00'}, {'id': 545, 'release_name': '中国民航大学第九届信息安全技术竞赛', 'release_sponsor': '中国民航大学', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-05-14 13:30:00', 'competition_end_time': '2023-05-14 17:00:00', 'sign_up_start_time': '2023-05-05 12:00:00', 'sign_up_end_time': '2023-05-13 20:00:00', 'release_cover': '', 'is_comment_open': 0, 'is_official': 1, 'hash': 'e9f5d5cc-eb20-11ed-b5da-000c296d9df6', 'competition_url': '', 'create_time': '2023-05-05 16:43:32', 'inside': 1, 'is_pass': 1, 'is_topping': 0, 'classes': 0, 'event_hash': {'event_hash': 'bcb2a112-ecab-11ed-ab28-000c29bc20bf', 'status': 1}, 'event_type': 1, 'event_status': 5, 'avg_rating': None, 'check_event': 0, 'check_xctf': {'type': 1, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-05-14 13:30:00', 'order_end_time': '2023-05-14 17:00:00'}, {'id': 547, 'release_name': '江河杯2023年东昌府区网络安全技能大赛', 'release_sponsor': '东昌府区区委网信办、区教体局、区卫健局、区人社局、区财政局、区公安分局', 'competition_type': 1, 'competition_mode': 1, 'competition_start_time': '2023-05-12 14:00:00', 'competition_end_time': '2023-05-14 15:00:00', 'sign_up_start_time': '2023-05-09 00:00:00', 'sign_up_end_time': '2023-05-12 00:00:00', 'release_cover': '/media/picture/cover/12e8e3c7-f117-47a0-8862-670472f070c1.png', 'is_comment_open': 0, 'is_official': 1, 'hash': 'a32a3e8e-efea-11ed-b5da-000c296d9df6', 'competition_url': 'http://jhb2023.ctf1.sierting.com/', 'create_time': '2023-05-11 18:57:36', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-05-12 14:00:00', 'order_end_time': '2023-05-14 15:00:00'}, {'id': 544, 'release_name': '商丘师范学院第三届网络安全及信息对抗大赛', 'release_sponsor': '共青团商丘师范学院委员会、信息技术学院、天乙信息安全实验室、青少年CTF', 'competition_type': 1, 'competition_mode': 4, 'competition_start_time': '2023-04-30 13:00:00', 'competition_end_time': '2023-04-30 18:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/c16bbec1-b585-4553-a0d9-cd2bb4467229.png', 'is_comment_open': 0, 'is_official': 1, 'hash': '98786b18-d9c0-11ed-ab28-000c29bc20bf', 'competition_url': 'https://tianyisec.feishu.cn/share/base/form/shrcn3F4IEvvVp3wFeBjCG7t6cb', 'create_time': '2023-04-13 14:01:13', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-04-30 13:00:00', 'order_end_time': '2023-04-30 18:00:00'}, {'id': 543, 'release_name': '商丘师范学院第三届网络安全及信息对抗大赛(线上个人赛)', 'release_sponsor': '共青团商丘师范学院委员会、商丘师范学院信息技术学院、商丘师范学院天乙信息安全实验室、青少年CTF', 'competition_type': 1, 'competition_mode': 1, 'competition_start_time': '2023-04-24 09:00:00', 'competition_end_time': '2023-04-30 18:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '', 'is_comment_open': 0, 'is_official': 1, 'hash': 'f2a43672-d9bf-11ed-ab28-000c29bc20bf', 'competition_url': 'https://tianyisec.feishu.cn/share/base/form/shrcn3F4IEvvVp3wFeBjCG7t6cb', 'create_time': '2023-04-13 13:56:35', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 0, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-04-24 09:00:00', 'order_end_time': '2023-04-30 18:00:00'}, {'id': 542, 'release_name': '第三届“红明谷”杯网络安全大赛', 'release_sponsor': '数字中国建设峰会组委会', 'competition_type': 9, 'competition_mode': 2, 'competition_start_time': '2023-04-15 00:00:00', 'competition_end_time': '2023-04-19 23:00:00', 'sign_up_start_time': '2023-03-25 00:00:00', 'sign_up_end_time': '2023-04-11 23:00:00', 'release_cover': '/media/picture/cover/b9d30b38-c2e7-42b7-a8b1-2f10db3e96a3.png', 'is_comment_open': 0, 'is_official': 2, 'hash': '72ea0dcc-d4f0-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-04-07 11:01:10', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-04-15 00:00:00', 'order_end_time': '2023-04-19 23:00:00'}, {'id': 541, 'release_name': '2022年第三届“网鼎杯”网络安全大赛(半决赛-总决赛)', 'release_sponsor': '杭州市公安局、杭州市余杭区人民政府', 'competition_type': 9, 'competition_mode': 4, 'competition_start_time': '2023-04-14 10:00:00', 'competition_end_time': '2023-04-14 18:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/81d513af-3eca-49dc-8cac-96e48da85cba.png', 'is_comment_open': 0, 'is_official': 2, 'hash': '3499f4bc-d4ee-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-04-07 10:45:07', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-04-14 10:00:00', 'order_end_time': '2023-04-14 18:00:00'}, {'id': 540, 'release_name': '2023数字中国创新大赛·数据安全产业人才能力挑战赛', 'release_sponsor': '数字中国建设峰会组委会', 'competition_type': 9, 'competition_mode': 2, 'competition_start_time': '2023-04-01 00:00:00', 'competition_end_time': '2023-04-01 23:00:00', 'sign_up_start_time': '2023-03-06 00:00:00', 'sign_up_end_time': '2023-03-22 23:00:00', 'release_cover': '/media/picture/cover/42fd938f-bce1-437c-8ccc-c68efb9a98e0.jpg', 'is_comment_open': 0, 'is_official': 2, 'hash': '98304184-c7b5-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-03-21 14:57:07', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-04-01 00:00:00', 'order_end_time': '2023-04-01 23:00:00'}, {'id': 530, 'release_name': '第七届XCTF国际网络攻防联赛总决赛', 'release_sponsor': 'XCTF国际网络攻防联赛组委会', 'competition_type': 9, 'competition_mode': 4, 'competition_start_time': '2023-03-29 09:30:00', 'competition_end_time': '2023-03-30 15:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/19034ed2-98be-4e93-aa9e-a5678de18726.jpg', 'is_comment_open': 0, 'is_official': 2, 'hash': '7d708254-bd89-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-03-08 16:16:13', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-29 09:30:00', 'order_end_time': '2023-03-30 15:00:00'}, {'id': 523, 'release_name': '齐鲁师范学院QLNU22级网络安全考核赛', 'release_sponsor': '齐鲁师范学院、天权信安网络安全团队', 'competition_type': 1, 'competition_mode': 1, 'competition_start_time': '2023-03-25 09:00:00', 'competition_end_time': '2023-03-25 17:00:00', 'sign_up_start_time': '2023-02-22 09:00:00', 'sign_up_end_time': '2023-03-24 17:00:00', 'release_cover': '/media/picture/cover/092a3e7e-20fe-4955-9bca-864cccfbfe68.png', 'is_comment_open': 0, 'is_official': 1, 'hash': '5b848f2a-b715-11ed-ab28-000c29bc20bf', 'competition_url': 'https://www.megrezsec.cn/game.html', 'create_time': '2023-02-28 11:09:48', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-25 09:00:00', 'order_end_time': '2023-03-25 17:00:00'}, {'id': 525, 'release_name': '2023数字中国创新大赛·数字网络安全人才挑战赛', 'release_sponsor': '数字中国建设峰会组委会', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-03-24 00:00:00', 'competition_end_time': '2023-03-25 00:00:00', 'sign_up_start_time': '2023-02-28 00:00:00', 'sign_up_end_time': '2023-03-20 00:00:00', 'release_cover': '/media/picture/cover/e277cab7-a949-40c7-b35f-f146613c2d6c.jpg', 'is_comment_open': 0, 'is_official': 2, 'hash': '1bbc2336-b7fa-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-03-01 14:27:15', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-24 00:00:00', 'order_end_time': '2023-03-25 00:00:00'}, {'id': 527, 'release_name': '2022第七届全国密码技术竞赛决赛', 'release_sponsor': '中国密码学会', 'competition_type': 1, 'competition_mode': 4, 'competition_start_time': '2023-03-17 08:30:00', 'competition_end_time': '2023-03-17 18:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '', 'is_comment_open': 0, 'is_official': 2, 'hash': '9cb4fe4e-b89b-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-03-02 09:43:21', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-17 08:30:00', 'order_end_time': '2023-03-17 18:00:00'}, {'id': 528, 'release_name': '2023年国家基地网络“攻&防”技能大赛', 'release_sponsor': '国家网络空间安全人才培养基地', 'competition_type': 9, 'competition_mode': 1, 'competition_start_time': '2023-03-16 08:00:00', 'competition_end_time': '2023-03-16 18:00:00', 'sign_up_start_time': '2023-02-02 00:00:00', 'sign_up_end_time': '2023-02-27 00:00:00', 'release_cover': '/media/picture/cover/6ee88514-aef5-4350-995c-528f65d8d3da.png', 'is_comment_open': 0, 'is_official': 2, 'hash': 'afee7dbe-b8a0-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-03-02 10:19:40', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-16 08:00:00', 'order_end_time': '2023-03-16 18:00:00'}, {'id': 524, 'release_name': '武交院eoe攻防实验室22级网络安全选拔赛', 'release_sponsor': '武汉交通职业学院、天权信安网络安全团队', 'competition_type': 1, 'competition_mode': 1, 'competition_start_time': '2023-03-14 14:00:00', 'competition_end_time': '2023-04-14 17:00:00', 'sign_up_start_time': '2023-02-22 09:00:00', 'sign_up_end_time': '2023-03-10 17:00:00', 'release_cover': None, 'is_comment_open': 0, 'is_official': 1, 'hash': '643ea15a-b715-11ed-ab28-000c29bc20bf', 'competition_url': 'https://www.megrezsec.cn/game.html', 'create_time': '2023-02-28 11:10:02', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 0, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-14 14:00:00', 'order_end_time': '2023-04-14 17:00:00'}, {'id': 518, 'release_name': 'picoCTF', 'release_sponsor': 'CMU', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-03-14 04:00:00', 'competition_end_time': '2023-03-28 08:00:00', 'sign_up_start_time': '2023-02-01 00:00:00', 'sign_up_end_time': '2023-03-13 00:00:00', 'release_cover': '/media/picture/cover/128b6de6-d26b-46bc-b506-495c4bd3bba1.png', 'is_comment_open': 0, 'is_official': 2, 'hash': 'eb5b6604-b18a-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-02-21 09:56:13', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-14 04:00:00', 'order_end_time': '2023-03-28 08:00:00'}, {'id': 517, 'release_name': 'hxp CTF 2022', 'release_sponsor': 'hxp', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-03-11 00:00:00', 'competition_end_time': '2023-03-13 00:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/b811866f-626e-463a-80d5-b775155c3115.png', 'is_comment_open': 0, 'is_official': 2, 'hash': '2279e1c2-a756-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-02-08 10:13:11', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-03-11 00:00:00', 'order_end_time': '2023-03-13 00:00:00'}, {'id': 510, 'release_name': 'pbctf 2023', 'release_sponsor': 'perfect blue', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-02-18 22:00:00', 'competition_end_time': '2023-02-20 10:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/7313db3f-f1a5-4394-ad5b-ff713dd85b0b.png', 'is_comment_open': 0, 'is_official': 2, 'hash': '419ff38e-9090-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-01-10 10:41:17', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-02-18 22:00:00', 'order_end_time': '2023-02-20 10:00:00'}, {'id': 514, 'release_name': '山东电专第一届网络安全竞赛暨2022级新生挑战赛', 'release_sponsor': '山东电力高等专科学校天基网络安全社团,思而听(山东)网络科技有限公司', 'competition_type': 1, 'competition_mode': 1, 'competition_start_time': '2023-02-18 08:00:00', 'competition_end_time': '2023-02-18 18:00:00', 'sign_up_start_time': '2023-01-13 00:00:00', 'sign_up_end_time': '2023-02-17 00:00:00', 'release_cover': '/media/picture/cover/6fe247eb-2ddd-4e5b-bfdd-9f74cf1dbc69.jpg', 'is_comment_open': 0, 'is_official': 1, 'hash': '26dfd1c0-a2ee-11ed-ab28-000c29bc20bf', 'competition_url': 'https://ctf.qsnctf.com/', 'create_time': '2023-02-02 19:38:46', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 0, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-02-18 08:00:00', 'order_end_time': '2023-02-18 18:00:00'}, {'id': 516, 'release_name': 'LA CTF 2023', 'release_sponsor': 'PBR | UCLA', 'competition_type': 1, 'competition_mode': 2, 'competition_start_time': '2023-02-11 12:00:00', 'competition_end_time': '2023-02-13 06:00:00', 'sign_up_start_time': '', 'sign_up_end_time': '', 'release_cover': '/media/picture/cover/e6808153-1e60-49a3-92b4-aef755aad411.png', 'is_comment_open': 0, 'is_official': 2, 'hash': 'd305dcf4-a755-11ed-ab28-000c29bc20bf', 'competition_url': None, 'create_time': '2023-02-08 10:10:57', 'inside': 0, 'is_pass': 1, 'is_topping': 0, 'classes': 1, 'event_hash': None, 'event_type': None, 'event_status': 5, 'avg_rating': None, 'check_event': 1, 'check_xctf': {'type': 0, 'xctf': 0}, 'is_test_event': 0, 'order_start_time': '2023-02-11 12:00:00', 'order_end_time': '2023-02-13 06:00:00'}], 'team_id': -1}}"
  • 1
  1. 标题
    写入文件一般都有标题。所以需要先定义标题,标题可以根据自己的需求定义,也可以向我一样直接根据数据返回的 key 来做标题。这里我贴一个我自己快速定义标题(获取key)的小方法。
    在这里插入图片描述
# 循环需要写入的数据。返回的是一组对象。
for item in result_data['data']['rows']:
    s = ""
    # 将可遍历的数据对象组合成一个索引序列。
    for i,t in enumerate(item):
        # 拼接成想要的数据
        s += f"'{t}',"
    # 输出结果即可
    print(s)
    # 只需要遍历一次即可
    break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. 写入 csv 文件
  • with open("data.csv",'w',newline='',encoding='utf-8') as csvfile:
    • 这行代码使用了Python内置的open函数来打开一个名为data.csv的CSV文件,并以写入模式(w)打开它。同时,使用newline=''参数来避免在Windows系统中出现空行的问题,使用encoding='utf-8'参数来指定文件编码为UTF-8,以确保写入的数据不会出现乱码。
  1. 展示
    在这里插入图片描述

后面的代码就如注释中说明的一样

持续更新中~~~

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号