Python-从日志文件读取字符串

时间:2018-12-03 09:16:43

标签: python python-3.x file numbers

我想从以下Python日志输出中提取serial_number值:

Continue? (y/N): Initializing nimble object...
##### Call time: -0.000003
Initializing nimble object...
##### Call time: 0.000002
##### Volume Records Retrieved From Nimble:
{u'parent_vol_name': u'', u'owned_by_group_id': u'00058a0dd7f9ecafd9000000000000000000000001', u'num_fc_connections': 4, u'dedupe_enabled': True, u'snap_usage_compressed_bytes': 3516972911, u'num_iscsi_connections': 0, u'move_bytes_remaining': 0, u'thinly_provisioned': True, u'cache_needed_for_pin': 107374182400, u'last_replicated_snap': None, u'space_usage_level': u'normal', u'fc_sessions': [{u'initiator_wwpn': u'51:40:2e:c0:01:ca:5c:d6', u'initiator_symbolic_nodename': u'', u'initiator_switch_port': u'20', u'initiator_symbolic_portname': u'', u'target_port_array_name': u'a1epc8snsm4001', u'target_wwnn': u'56:c9:ce:90:d4:51:6d:00', u'target_wwpn': u'56:c9:ce:90:d4:51:6d:06', u'initiator_alias': u'a1epc8lhan402_hba1_p2', u'session_id': u'330000000051402ec001ca5cd656c9ce90d4516d06', u'alua': u'standby', u'pr_key': 0, u'target_port_ctrlr_id': 1, u'initiator_fcid': 136192, u'target_port_interface_name': u'fc2b.1', u'target_fcid': 131328, u'initiator_switch_name': u'a1epc8sfcs4002', u'id': u'330000000051402ec001ca5cd656c9ce90d4516d06'}, {u'initiator_wwpn': u'51:40:2e:c0:01:ca:5c:a4', u'initiator_symbolic_nodename': u'', u'initiator_switch_port': u'20', u'initiator_symbolic_portname': u'', u'target_port_array_name': u'a1epc8snsm4001', u'target_wwnn': u'56:c9:ce:90:d4:51:6d:00', u'target_wwpn': u'56:c9:ce:90:d4:51:6d:05', u'initiator_alias': u'a1epc8lhan402_hba2_p1', u'session_id': u'330000000051402ec001ca5ca456c9ce90d4516d05', u'alua': u'standby', u'pr_key': 0, u'target_port_ctrlr_id': 1, u'initiator_fcid': 70656, u'target_port_interface_name': u'fc2a.1', u'target_fcid': 65792, u'initiator_switch_name': u'a1epc8sfcs4001', u'id': u'330000000051402ec001ca5ca456c9ce90d4516d05'}, {u'initiator_wwpn': u'51:40:2e:c0:01:ca:5c:d6', u'initiator_symbolic_nodename': u'', u'initiator_switch_port': u'20', u'initiator_symbolic_portname': u'', u'target_port_array_name': u'a1epc8snsm4001', u'target_wwnn': u'56:c9:ce:90:d4:51:6d:00', u'target_wwpn': u'56:c9:ce:90:d4:51:6d:02', u'initiator_alias': u'a1epc8lhan402_hba1_p2', u'session_id': u'330000000051402ec001ca5cd656c9ce90d4516d02', u'alua': u'active_optimized', u'pr_key': 0, u'target_port_ctrlr_id': 0, u'initiator_fcid': 136192, u'target_port_interface_name': u'fc2b.1', u'target_fcid': 131072, u'initiator_switch_name': u'a1epc8sfcs4002', u'id': u'330000000051402ec001ca5cd656c9ce90d4516d02'}, {u'initiator_wwpn': u'51:40:2e:c0:01:ca:5c:a4', u'initiator_symbolic_nodename': u'', u'initiator_switch_port': u'20', u'initiator_symbolic_portname': u'', u'target_port_array_name': u'a1epc8snsm4001', u'target_wwnn': u'56:c9:ce:90:d4:51:6d:00', u'target_wwpn': u'56:c9:ce:90:d4:51:6d:01', u'initiator_alias': u'a1epc8lhan402_hba2_p1', u'session_id': u'330000000051402ec001ca5ca456c9ce90d4516d01', u'alua': u'active_optimized', u'pr_key': 0, u'target_port_ctrlr_id': 0, u'initiator_fcid': 70656, u'target_port_interface_name': u'fc2a.1', u'target_fcid': 65536, u'initiator_switch_name': u'a1epc8sfcs4001', u'id': u'330000000051402ec001ca5ca456c9ce90d4516d01'}], u'vol_usage_uncompressed_bytes': 4968947712, u'num_snaps': 4, u'base_snap_name': u'', u'cache_pinned': False, u'name': u'a1epc8lhan402-boot-2', u'num_connections': 4, u'last_content_snap_id': 0, u'cksum_last_verified': 0, u'avg_stats_last_5mins': {u'read_latency': 0, u'combined_throughput': 816, u'read_throughput': 0, u'write_latency': 4, u'write_throughput': 816, u'combined_iops': 0, u'read_iops': 0, u'write_iops': 0, u'combined_latency': 4}, u'usage_valid': True, u'creation_time': 1543442584, u'full_name': u'default:/a1epc8lhan402/a1epc8lhan402-boot-2', u'move_bytes_migrated': 0, u'snap_reserve': 0, u'move_est_compl_time': 0, u'volcoll_id': u'07058a0dd7f9ecafd9000000000000000000000004', u'vol_usage_compressed_bytes': 2477152400, u'perfpolicy_name': u'hana4k-data', u'agent_type': u'none', u'base_snap_id': u'', u'metadata': None, u'app_category': u'Other', u'cache_policy': u'normal', u'encryption_cipher': u'none', u'online_snaps': None, u'last_modified': 1543442600, u'snap_limit_percent': -1, u'folder_id': u'2f058a0dd7f9ecafd9000000000000000000000002', u'total_usage_bytes': 5994125311, u'iscsi_sessions': None, u'snap_limit': 9223372036854775807, u'pool_id': u'0a058a0dd7f9ecafd9000000000000000000000001', u'snap_usage_populated_bytes': 21854855168, u'needs_content_repl': False, u'move_start_time': 0, u'warn_level': 80, u'offline_reason': None, u'dest_pool_name': u'', u'block_size': 4096, u'size': 102400, u'perfpolicy_id': u'03058a0dd7f9ecafd900000000000000000000001f', u'move_aborting': False, u'pinned_cache_size': 0, u'serial_number': u'7e89fa94a3829e8b6c9ce9000ea266fc', u'limit_iops': -1, u'protection_type': u'local', u'folder_name': u'a1epc8lhan402', u'vpd_t10': u'Nimble  7e89fa94a3829e8b6c9ce9000ea266fc', u'limit': 100, u'app_uuid': u'', u'projected_num_snaps': 0, u'last_snap': {u'snap_id': u'04058a0dd7f9ecafd9000000000000005c00005343', u'snap_creation_time': 1543726800, u'snap_name': u'Boot-Policy-Boot-Daily-2018-12-02::00:00:00.000'}, u'target_name': u'56:c9:ce:90:d4:51:6d:00', u'dest_pool_id': u'', u'id': u'06058a0dd7f9ecafd9000000000000000000000057', u'read_only': False, u'volcoll_name': u'Boot-Policy', u'content_repl_errors_found': False, u'multi_initiator': True, u'last_content_snap_br_cg_uid': 0, u'owned_by_group': u'a1epc8snsm4001-grp', u'snap_usage_uncompressed_bytes': 7053840384, u'online': True, u'access_control_records': [{u'chap_user_name': u'*', u'vol_id': u'06058a0dd7f9ecafd9000000000000000000000057', u'pe_name': u'', u'snapluns': None, u'acl_id': u'0d058a0dd7f9ecafd900000000000000000000005a', u'initiator_group_id': u'02058a0dd7f9ecafd9000000000000000000000008', u'access_protocol': u'fc', u'chap_user_id': u'', u'initiator_group_name': u'a1epc8lhan402', u'vol_name': u'a1epc8lhan402-boot-2', u'apply_to': u'both', u'pe_id': u'', u'pe_lun': None, u'id': u'0d058a0dd7f9ecafd900000000000000000000005a', u'lun': 0}], u'caching_enabled': True, u'pool_name': u'default', u'description': u'', u'clone': False, u'search_name': u'a1epc8lhan402-boot-2', u'snap_warn_level': 0, u'last_content_snap_br_gid': 0, u'previously_deduped': True, u'parent_vol_id': u'', u'limit_mbps': -1, u'upstream_cache_pinned': False, u'vpd_ieee0': u'7e89fa94a3829e8b', u'vpd_ieee1': u'6c9ce9000ea266fc', u'vol_state': u'online', u'reserve': 0}

如何做到这一点?

预先感谢

1 个答案:

答案 0 :(得分:0)

只要文本文件的格式与您发布的Python日志输出的格式相同(可以全部返回),就可以使用简单的正则表达式将其提取出来:

import re
file="".join([i for i in open("yourfileinthesamefolder.txt")])
serials=re.findall("u'serial_number': u'(.+)'",file)
print(serials)

我建议阅读有关如何在Python中使用正则表达式的内容:

Regular Expression HOWTO — Python 3.7.2 documentation