response = requests.get(url, params=params, headers=headers, stream=True) response.raise_for_status() for line in response.iter_lines(): if line: decoded_line = line.decode("utf-8").lstrip("data: ") data = json.loads(decoded_line) for de in data["data"]["details"]: ts, jg, cj, _, _ = de.split(",") print(f"时间:{ts} 成交:{cj} 价格:{jg}")
局部符号表 locals()
1 2 3 4 5 6 7 8
defmy_function(): a = 10 b = 20 print(locals()) # 打印当前函数的局部变量
withopen("spus.jsonl", encoding="utf8") as fp: for item in jsonlines.Reader(fp): print(item)
openpyxl
读取表格
1 2 3 4 5 6 7
workbook = load_workbook(file_path) sheet = workbook.active # 获取当前工作簿的活动工作表 for row in sheet.iter_rows( values_only=True, # 只返回每行的值 min_row=2# 从第二行开始读,跳过标题 ): data.append(row)
写入表格
1 2 3 4 5
workbook = Workbook() sheet = workbook.active for row in data: sheet.append(row) workbook.save("cxs.xlsx")
单元格颜色
1 2 3 4 5 6 7 8 9 10 11
# 选择单元格 cell = ws['C1'] # 获取填充对象 fill = cell.fill # 检查填充类型 if fill.fill_type == 'solid': # 获取填充颜色 color = fill.start_color print(f"The color of cell A1 is {color.rgb}") else: print("Cell A1 does not have a solid fill color.")
zipfile
解压缩
1 2
with zipfile.ZipFile(zip_file, "r") as zip_ref: zip_ref.extractall(file_path)