求解什么会有这个报错,我明明把不存在的key都对应了value值“无数值”
Traceback (most recent call last):File "C:/Users/JPQ/PycharmProjects/untitled/爬虫/新方案用于分析数据.py", line 34, in <module>
insertData = , dict["颜色"], dict["浊度"], dict["[尿液分析+]"] , dict["尿糖"],dict["尿胆红素"],dict["尿酮体"],dict["尿比重"],dict["尿隐血"],dict["尿酸碱度"],dict["尿蛋白"],dict["尿胆原"],dict["尿亚硝酸盐"],dict["白细胞酯酶"],dict["红细胞"],dict["白细胞"],dict["上皮细胞"],dict["小圆上皮细胞"],dict["管型"],dict["病理管型"],dict["红细胞(高倍视野)"],dict["白细胞(高倍视野)"],dict["细菌"],dict["上皮细胞(高倍视野)"],dict["管型(低倍视野)"],dict["细菌(高倍视野)"]]
KeyError: '类酵母菌'
['类酵母菌', '[尿液分析+]', '颜色', '浊度', '尿糖', '尿胆红素', '尿酮体', '尿比重', '尿隐血', '尿酸碱度', '尿蛋白', '尿胆原', '尿亚硝酸盐', '白细胞酯酶', '红细胞', '白细胞', '上皮细胞', '小圆上皮细胞', '管型', '病理管型', '红细胞(高倍视野)', '白细胞(高倍视野)', '细菌', '上皮细胞(高倍视野)', '管型(低倍视野)', '细菌(高倍视野)']
<class 'list'>
['类酵母菌', '[尿液分析+]', '颜色', '浊度', '尿糖', '尿胆红素', '尿酮体', '尿比重', '尿隐血', '尿酸碱度', '尿蛋白', '尿胆原', '尿亚硝酸盐', '白细胞酯酶', '红细胞', '白细胞', '上皮细胞', '小圆上皮细胞', '管型', '病理管型', '红细胞(高倍视野)', '白细胞(高倍视野)', '细菌', '上皮细胞(高倍视野)', '管型(低倍视野)', '细菌(高倍视野)']
<class 'list'>
Process finished with exit code 1
import pandas as pd
import xlsxwriter as xw
list1=[]
list2=[]
file_path="C:\\Users\\JPQ\\Desktop\\数据处理\\2019-1上半年.xlsx"
data=pd.read_excel(file_path)
for n in range(len(data)):
if data.at,"location"]==0:
list1.append(n)
df1=data.loc[:,["PNAME","CHINNAME","SVALUE"]]
# print(df1)
dict={}
workbook = xw.Workbook("fileName")# 创建工作簿
worksheet1 = workbook.add_worksheet("sheet1")# 创建子表
worksheet1.activate()# 激活表
title = ["类酵母菌","颜色","浊度","[尿液分析+]","尿糖","尿胆红素","尿酮体","尿比重","尿隐血","尿酸碱度","尿蛋白","尿胆原","尿亚硝酸盐","白细胞酯酶","红细胞","白细胞","上皮细胞","小圆上皮细胞","管型","病理管型","红细胞(高倍视野)","白细胞(高倍视野)","细菌","上皮细胞(高倍视野)","管型(低倍视野)","细菌(高倍视野)"]# 设置表头
worksheet1.write_row('B1', title)
for i in range(6042,9012):
a=list1
b=list11]-1
data2=df1
dict=data2[["CHINNAME", "SVALUE"]].set_index("CHINNAME").to_dict()["SVALUE"]
list3=list(dict.keys())
print(list3)
print(type(list3))
# print(len(list3))
for n in list3:
if n not in title:
dict="无数据"
insertData = "类酵母菌"], dict["颜色"], dict["浊度"], dict["[尿液分析+]"] , dict["尿糖"],dict["尿胆红素"],dict["尿酮体"],dict["尿比重"],dict["尿隐血"],dict["尿酸碱度"],dict["尿蛋白"],dict["尿胆原"],dict["尿亚硝酸盐"],dict["白细胞酯酶"],dict["红细胞"],dict["白细胞"],dict["上皮细胞"],dict["小圆上皮细胞"],dict["管型"],dict["病理管型"],dict["红细胞(高倍视野)"],dict["白细胞(高倍视野)"],dict["细菌"],dict["上皮细胞(高倍视野)"],dict["管型(低倍视野)"],dict["细菌(高倍视野)"]]
dict.clear()
list3.clear()
row="B"+str(i+1)
worksheet1.write_row(row, insertData)
workbook.close() 本帖最后由 fanvalen 于 2021-6-5 21:33 编辑
没有数据不能调试
不过你要worksheet1.write_row(row, insertData)在之前打印一下insertData来看看
dict 是 python的 关键字,你却把它当做变量,先换个变量名再说,没见过这么干的 ReLoading 发表于 2021-6-5 22:34
dict 是 python的 关键字,你却把它当做变量,先换个变量名再说,没见过这么干的
换了变量名还是不行
页:
[1]