题目:d.values(),返回的是什么类型呢?

本文详细介绍了Python中字典值(dict_values)类型的使用方法。通过示例代码展示了如何获取字典的所有值,并解释了dict_values类型的特点。对于理解Python字典操作及数据类型转换有重要作用。
部署运行你感兴趣的模型镜像

答案是:dict_values类型,包括字典中所有值

d  = {'a':1,'b':2,'c':3}
c = d.values()
print(c)        #dict_values([1, 2, 3])
print(type(c))  #返回:字典值(dict_values)类型
print(type(d))  #返回:字典(dict)类型

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 完整的插件脚本,能够检测并报告所有类型的错误,按问题类型分组,并将连续时间戳合并为时间段 """ import os import Hubble.utils.basic.HiviewLogger as HiviewLogger import re import datetime from datetime import datetime, timedelta import json class PluginClass: def __init__(self): self.logger = HiviewLogger.HiviewLogger().get_logger() self.plugin_result = { "conclusion": { "level_one": "", "level_two": "", "reason_info": "", "solution": "", "weight_level": "", }, "analysis_process": { "show_order": ["1", "2"], "detailed_process": { "1": {"details": "", "CN": ""}, "2": {"details": "", "CN": ""} } }, "feature_data": {"": []} } def log_str_to_datetime(self, time_str): try: issue_time = datetime.strptime(time_str, '%m-%d %H:%M:%S.%f') current_year = datetime.now().year return issue_time.replace(year=current_year) except ValueError: try: issue_time = datetime.strptime(time_str, '%m-%d %H:%M:%S') current_year = datetime.now().year return issue_time.replace(year=current_year) except Exception: return datetime.now() def merge_time_ranges(self, events, max_gap_minutes=5): """修复的时间段合并函数""" if not events: return [] # 按时间戳排序 sorted_events = sorted(events, key=lambda x: self.log_str_to_datetime(x["timestamp"])) merged_ranges = [] # 初始化第一个时间段 current_start = self.log_str_to_datetime(sorted_events[0]["timestamp"]) current_end = current_start current_events = [sorted_events[0]] # 存储当前时间段的事件 for i in range(1, len(sorted_events)): event = sorted_events[i] event_time = self.log_str_to_datetime(event["timestamp"]) # 计算与前一个事件的时间间隔 gap = event_time - current_end # 如果间隔小于阈值,则扩展当前时间段 if gap.total_seconds() <= max_gap_minutes * 60: current_end = event_time current_events.append(event) else: # 保存当前时间段 merged_ranges.append({ "start": current_start, "end": current_end, "count": len(current_events), "events": current_events.copy() # 复制当前事件列表 }) # 开始新的时间段 current_start = event_time current_end = event_time current_events = [event] # 添加最后一个时间段 merged_ranges.append({ "start": current_start, "end": current_end, "count": len(current_events), "events": current_events }) return merged_ranges # 以下是各种问题检测函数(保持不变) def memAvailable(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*curBufKB=(?P<curBufKB>\d+)' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") memAvailable_value = int(match.group('curBufKB')) if memAvailable_value < 819200: results.append({"type": "memAvailable", "timestamp": timestamp, "value": memAvailable_value}) return results def am_kill(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*KillOneProc kill_reason=LowMemoryKill.*procName=(?P<package>[a-zA-Z0-9._]+)' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") kill_proc_name = match.group("package") results.append({"type": "am_kill", "timestamp": timestamp, "proc_name": kill_proc_name}) return results def temperature(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*new temp: (?P<new_temp>\d+)' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") temp_value = int(match.group("new_temp")) if temp_value > 43: results.append({"type": "temperature", "timestamp": timestamp, "value": temp_value}) return results def storage(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*roundSize=(?P<round_size>\d+),.*freeSize=(?P<free_size>\d+)' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") round_size = int(match.group("round_size")) free_size = int(match.group("free_size")) if round_size > 0: ratio = free_size / round_size if ratio <= 0.15: results.append({"type": "storage", "timestamp": timestamp, "ratio": ratio}) return results def free_sec(self, hilog_kmsg): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*free_sec=(?P<free_sec>\d+).*Free =(?P<free>\d+)MB' for line in hilog_kmsg: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") free_sec_value = int(match.group("free_sec")) free_value = int(match.group("free")) if free_value > 0: ratio = 1 - (free_sec_value * 2) / free_value if ratio > 0.8: results.append({"type": "free_sec", "timestamp": timestamp, "ratio": ratio}) return results def ps_slow(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*CHR name=(PS_SLOW_EVENT), type' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") results.append({"type": "ps_slow", "timestamp": timestamp}) return results def power_low(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*powermgr/BatteryInfo: capacity=(?P<capacity>\d+), voltage' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") capacity_value = int(match.group("capacity")) if capacity_value < 10: results.append({"type": "power_low", "timestamp": timestamp, "value": capacity_value}) return results def voltage(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*powermgr/BatteryInfo: .*voltage=(?P<voltage>\d+), temperature' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") voltage_value = int(match.group("voltage")) if voltage_value < 3500000: results.append({"type": "voltage", "timestamp": timestamp, "value": voltage_value}) return results def cup_load(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*cpuLoadPercent:(?P<cpuLoadPercent>\d+), thermalLevel' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") try: cpu_value = int(match.group("cpuLoadPercent")) if cpu_value > 600000: results.append({"type": "cup_load", "timestamp": timestamp, "value": cpu_value}) except ValueError: continue return results def gpu_load(self, hilog): results = [] pattern = r'(?P<timestamp>\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*Get GpuTotalUsed value is (?P<gpuUsed>\d+)' for line in hilog: match = re.search(pattern, line) if match: timestamp = match.group("timestamp") try: gpu_value = int(match.group("gpuUsed")) if gpu_value > 600000: results.append({"type": "gpu_load", "timestamp": timestamp, "value": gpu_value}) except ValueError: continue return results def plugin_preprocess(self, info_dict=None): self.logger.info("预处理函数执行中...") task_list = [] download_list = info_dict["common_info"]["log_download_list"] max_analysis_num = 1 if len(download_list) > max_analysis_num: download_list = download_list[:max_analysis_num] for download_item in download_list: task_dict = {"download_addr": [], "task_params": {}} task_dict["download_addr"] = download_item task_list.append(task_dict) return task_list def run_plugin_single(self, uuid_log_path=None, log_path=None, param_dict=None): self.logger.info("分析脚本执行中...uuid_log_path=[%s]", uuid_log_path) error_mapping = { "memAvailable": {"reason": "整机低内存(RAM)", "solution": "1、清除后台应用:桌面手势导航底部上划进入多任务中心,清理后台多任务应用卡片(三键导航点击导航键的方块图标可以进入多任务界面)2、长按电源键关机重启"}, "am_kill": {"reason": "应用因低内存被查杀", "solution": "1、清除后台应用:桌面手势导航底部上划进入多任务中心,清理后台多任务应用卡片(三键导航点击导航键的方块图标可以进入多任务界面)2、长按电源键关机重启"}, "temperature": {"reason": "整机温度过高", "solution": "建议参考手机/平板使用过程中设备发热相关知识排查"}, "storage": {"reason": "整机可用存储空间不足", "solution": "建议参考'华为手机/平板内存占用多,提示内存不足如何处理?'知识中场景一:存储剩余空间不足排查方案处理。"}, "free_sec": {"reason": "整机碎片化过高", "solution": "1、建议引导用户进入设置>存储,进行整机清理加速,清理不需要的垃圾文件、联网缓存及不常用的应用;2、建议用户尝试夜间熄屏充电,持续此操作2-3晚上清理碎片化,促进系统优化"}, "ps_slow": {"reason": "整机网络不佳", "solution": "当前所处网络环境覆盖较弱或干扰较大,建议引导用户更换网络环境"}, "power_low": {"reason": "整机低电量", "solution": "建议在电量充足情况下使用"}, "voltage": {"reason": "整机低电压", "solution": "建议检查电池健康度并在电量充足情况下继续使用体验"}, "cup_load": {"reason": "整机CPU高负载", "solution": "1、清除后台应用:桌面手势导航底部上划进入多任务中心,清理后台多任务应用卡片(三键导航点击导航键的方块图标可以进入多任务界面);2、引导用户排查是否存在高速下载、 数据传输、多悬浮窗口使用等场景,以上高负载场景会导致设备卡顿,建议用户避免长时间使用上述场景;3、长按电源键关机重启;"}, "gpu_load": {"reason": "整机GPU高负载", "solution": "1、清除后台应用:桌面手势导航底部上划进入多任务中心,清理后台多任务应用卡片(三键导航点击导航键的方块图标可以进入多任务界面);2、引导用户排查是否存在高速下载、 数据传输、多悬浮窗口使用等场景,以上高负载场景会导致设备卡顿,建议用户避免长时间使用上述场景;3、长按电源键关机重启;"} } app_path = [] kmsg_path = [] for root, dirs, files in os.walk(uuid_log_path): for file in files: if "hilog." in file: app_path.append(os.path.join(root, file)) if "hilog_kmsg" in file: kmsg_path.append(os.path.join(root, file)) app_list = [] for app in app_path: try: with open(app, "r", encoding="utf-8", errors='ignore') as app_reader: app_list += app_reader.readlines() except Exception as e: self.logger.error(f"读取日志文件失败: {e}") kmsg_list = [] for kmsg in kmsg_path: try: with open(kmsg, "r", encoding="utf-8", errors='ignore') as kmsg_reader: kmsg_list += kmsg_reader.readlines() except Exception as e: self.logger.error(f"读取kmsg日志失败: {e}") detected_issues = { "memAvailable": self.memAvailable(app_list), "am_kill": self.am_kill(app_list), "temperature": self.temperature(app_list), "storage": self.storage(app_list), "free_sec": self.free_sec(kmsg_list), "ps_slow": self.ps_slow(app_list), "power_low": self.power_low(app_list), "voltage": self.voltage(app_list), "cup_load": self.cup_load(app_list), "gpu_load": self.gpu_load(app_list) } merged_issues = {} for issue_type, issues in detected_issues.items(): if issues: merged_issues[issue_type] = self.merge_time_ranges(issues) report_by_type = {} solutions = set() for issue_type, time_ranges in merged_issues.items(): if not time_ranges: continue issue_report = [] for time_range in time_ranges: start_time = time_range["start"].strftime("%m-%d %H:%M:%S") end_time = time_range["end"].strftime("%m-%d %H:%M:%S") if time_range["count"] == 1: time_info = f"{start_time}" else: time_info = f"{start_time} 至 {end_time} ({time_range['count']}次)" details = error_mapping[issue_type]["reason"] if issue_type == "am_kill": apps = list(set(event.get("proc_name", "未知应用") for event in time_range["events"])) details += f" ({', '.join(apps)})" elif issue_type in ["memAvailable", "temperature", "power_low", "voltage", "cup_load", "gpu_load"]: values = [event.get("value", 0) for event in time_range["events"]] min_val = min(values) max_val = max(values) avg_val = sum(values) / len(values) unit = "" if issue_type == "memAvailable": unit = "KB" elif issue_type == "temperature": unit = "°C" elif issue_type == "power_low": unit = "%" elif issue_type == "voltage": unit = "mV" details += f" (值范围: {min_val}-{max_val}{unit}, 平均: {avg_val:.1f}{unit})" elif issue_type in ["storage", "free_sec"]: ratios = [event.get("ratio", 0) for event in time_range["events"]] avg_ratio = sum(ratios) / len(ratios) * 100 details += f" (平均: {avg_ratio:.1f}%)" issue_report.append(f"{time_info}: {details}") report_by_type[issue_type] = issue_report solutions.add(error_mapping[issue_type]["solution"]) reason_info = [] if not report_by_type: reason_info.append("当前整机各项指标未诊断出异常") else: for issue_type, issue_list in report_by_type.items(): reason_info.append(f"<b>{error_mapping[issue_type]['reason']}</b>") reason_info.extend(issue_list) reason_info.append("") solution_str = "<br>".join(solutions) if solutions else"建议引导用户重新复现问题反馈日志" self.plugin_result["conclusion"]["reason_info"] = "<br>".join(reason_info) self.plugin_result["conclusion"]["solution"] = solution_str self.plugin_result["conclusion"]["level_one"] = "稳定性问题" self.plugin_result["conclusion"]["weight_level"] = "medium" return self.plugin_result def plugin_result_refinement(self, result): return result 这是我现在的脚本,已经没有异常了,目前整机温度过高显示的是 09-15 12:53:28: 整机温度过高 (值范围: 44-69°C, 平均: 48.7°C),这个数值收集的有点问题,日志是这样的 16598: 04-17 18:33:07.767 636 60287 I C02D15/hiview/XPower: [task_148]#ThermalDaemon:[shell_frame] new temp: 36 old temp: 37 需要收集的值是[shell_frame] new temp:后面的数字,但是[shell_front] new temp:后面的也一块收集了,现在只收集[shell_frame] new temp:后面的数字,43度以上显示显示方式原封不动,48度以上的不要显示温度是多少,直接显示温度异常,顺便帮我加一下注释,然后帮我返回完整代码:
09-17
import psycopg2 import pyodbc import re import logging import json # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def extract_transform_load(): try: # 1. 连接PostgreSQL pg_conn = psycopg2.connect( host="192.168.154.11", port="5432", database="andondata", user="powertrain", password="andromeda" ) pg_cursor = pg_conn.cursor() pg_cursor.execute(""" SELECT data, "createdAt", "updatedAt" FROM line1assembly ORDER BY "createdAt" DESC LIMIT 1 """) rows = pg_cursor.fetchall() logger.info(f"从PostgreSQL获取了 {len(rows)} 条记录") # 2. 数据转换 - 添加列表类型支持 transformed_data = [] for row in rows: if len(row) < 3: logger.warning(f"跳过无效行: 缺少必要字段 - {row}") continue raw_data, created_at, updated_at = row # 处理列表类型数据 if isinstance(raw_data, list): logger.info("检测到列表类型数据") for item in raw_data: # 处理嵌套列表 if isinstance(item, list) and len(item) >= 2: code = str(item[0]).strip() if item[0] is not None else None hex_str = str(item[1]).strip() if len(item) > 1 and item[1] is not None else None if code and hex_str: try: data_value = int(hex_str, 16) transformed_data.append(('12组装线', code, data_value, created_at, updated_at)) logger.info(f"列表处理: 添加 {code} = {data_value}") except ValueError: logger.error(f"无效的十六进制值: '{hex_str}'") else: logger.warning(f"列表项缺少必要值: {item}") else: logger.warning(f"跳过无效列表项: {item}") # 处理字符串类型数据 elif isinstance(raw_data, (str, bytes)): logger.info("检测到字符串/字节类型数据") if isinstance(raw_data, bytes): raw_str = raw_data.decode('utf-8', errors='replace') else: raw_str = raw_data # 尝试两种格式匹配 formats = [ r'\{([^,]+),([^}]+)\}', # 原始格式: {code, hex} r'\[\s*\[\s*"([^"]+)",\s*"([^"]+)"\s*\]\s*\]' # 新格式: [["code", "hex"]] ] found = False for fmt in formats: pattern = re.compile(fmt) matches = pattern.findall(raw_str) if matches: found = True logger.info(f"使用格式 '{fmt}' 找到 {len(matches)} 个匹配项") for match in matches: if len(match) >= 2: code = match[0].strip() hex_str = match[1].strip() try: data_value = int(hex_str, 16) transformed_data.append(('12组装线', code, data_value, created_at, updated_at)) logger.info(f"匹配添加: {code} = {data_value}") except ValueError: logger.error(f"无效的十六进制值: '{hex_str}'") else: logger.warning(f"跳过无效匹配: {match}") if not found: logger.warning(f"未匹配到任何格式: {raw_str[:100]}...") # 处理字典/JSON类型数据 elif isinstance(raw_data, dict): logger.info("检测到字典类型数据") try: # 尝试从字典中提取值 code = str(raw_data.get('code', raw_data.get('Code', ''))).strip() hex_str = str(raw_data.get('hex', raw_data.get('Hex', raw_data.get('value', '')))).strip() if code and hex_str: try: data_value = int(hex_str, 16) transformed_data.append(('12组装线', code, data_value, created_at, updated_at)) logger.info(f"字典处理: 添加 {code} = {data_value}") except ValueError: logger.error(f"无效的十六进制值: '{hex_str}'") except Exception as e: logger.error(f"处理字典数据时出错: {str(e)}") else: logger.error(f"不支持的数据类型: {type(raw_data)} - 值: {raw_data}") # 3. 连接SQL Server(与之前相同) if not transformed_data: logger.warning("没有可转换的数据,跳过SQL Server操作") return sql_conn = pyodbc.connect( 'DRIVER={ODBC Driver 17 for SQL Server};' 'SERVER=192.168.10.79;' 'DATABASE=TNGA物流DB;' 'UID=sa;' 'PWD=zhglbQG@)!$6600' ) sql_cursor = sql_conn.cursor() # 创建目标表(如果不存在) sql_cursor.execute(""" IF NOT EXISTS ( SELECT * FROM information_schema.tables WHERE table_name = 'dwd_webandon' ) CREATE TABLE dwd_webandon ( line VARCHAR(20), code VARCHAR(20), data_value DECIMAL(38,0), createdAt DATETIME, updatedAt DATETIME ) """) # 批量插入数据 insert_query = """ INSERT INTO dwd_webandon (line, code, data_value, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?) \ """ sql_cursor.executemany(insert_query, transformed_data) sql_conn.commit() logger.info(f"成功插入 {len(transformed_data)} 条记录到SQL Server") except Exception as e: logger.exception("ETL过程中发生错误") finally: # 确保关闭所有连接 try: pg_cursor.close() pg_conn.close() except: pass try: sql_cursor.close() sql_conn.close() except: pass if __name__ == "__main__": extract_transform_load() 修改以上python脚本,其中抽取后的hex_str为二进制数据且长度为16位,将hex_str中的每4位二进制数据转换为一个16进制的数据,转换后的结果为长度为4位的数据
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

京城徐董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值