#encoding:utf-8
import json
import requests
import xlwt
def readFile():
with open("cart.json", "r",encoding='utf-8') as f:
results = ''
for data in f.readlines():
dataJson = json.loads(data)
results=dataJson['results']
titles = ["host","title","ip","domain","port","country","province","city","country_name","header","server","protocol","banner","cert","isp","as_number","as_organization","latitude","longitude","icp","fid","cname"]
print(results[0])
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet')
# 写入excel
for i in range(len(titles)):
# print(titles[i])
# 参数对应 行, 列, 值
worksheet.write(0, i, label=titles[i])
j = 1
for i in range(len(results)):
item = results[i]
for k in range(len(item)):
print(j,k,item[k])
# 参数对应 行, 列, 值
worksheet.write(j, k, label=item[k])
j+=1
# 保存
workbook.save('Excel_test.xls')
readFile()
更多文档:
Python 对Excel文件 读取、写入、追加操作
https://blog.csdn.net/nanhuaibeian/article/details/99990961
读取报错
pandas无法打开.xlsx文件,xlrd.biffh.XLRDError: Excel xlsx file; not supported
原因是最近xlrd更新到了2.0.1版本,只支持.xls文件。所以pandas.read_excel(‘xxx.xlsx’)会报错。
可以安装旧版xlrd,在cmd中运行:
pip uninstall xlrd
pip install xlrd==1.2.0
也可以用openpyxl代替xlrd打开.xlsx文件:
df=pandas.read_excel(‘data.xlsx’,engine=‘openpyxl’)