我有一个来自库的代码的和平,它会发出警告:
report_methods = {
"missing_bus_indices": diag_report.report_missing_bus_indices,
"disconnected_elements": diag_report.report_disconnected_elements,
"different_voltage_levels_connected": diag_report.report_different_voltage_levels_connected,
"impedance_values_close_to_zero": diag_report.report_impedance_values_close_to_zero,
"nominal_voltages_dont_match": diag_report.report_nominal_voltages_dont_match,
"invalid_values": diag_report.report_invalid_values,
"overload": diag_report.report_overload,
"multiple_voltage_controlling_elements_per_bus": diag_report.report_multiple_voltage_controlling_elements_per_bus,
"wrong_switch_configuration": diag_report.report_wrong_switch_configuration,
"no_ext_grid": diag_report.report_no_ext_grid,
"wrong_reference_system": diag_report.report_wrong_reference_system,
"deviation_from_std_type": diag_report.report_deviation_from_std_type,
"numba_comparison": diag_report.report_numba_comparison,
"parallel_switches": diag_report.report_parallel_switches
}
# separator between log messages
log_message_sep = ("\n --------\n")
logger.warning("\n\n_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n")
for key in report_methods:
if (key in diag_results) :
report_methods[key]()
logger.warning(log_message_sep)
logger.warning("_____________ END OF PANDAPOWER DIAGNOSTIC _____________ ")下面是我在python控制台中得到的结果:
_____________ PANDAPOWER DIAGNOSTIC TOOL _____________
--------
disconnected_elements:
disonnected_section: {'buses': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122], 'lines': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120], 'loads': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120]}
--------
--------
impedance_values_close_to_zero:
line 0: r_ohm <= 0.0005 or x_ohm <= 0.0001
--------
--------
invalid_values:
line:
line 0: 'length_km' = 0.0 (restriction: >0)
--------
--------
--------
--------
--------
--------
--------
--------
--------
_____________ END OF PANDAPOWER DIAGNOSTIC _____________ 我想用我尝试过的f.write将这个内容写入txt文件:
for key in report_methods:
if (key in diag_results) :
if not report_methods[key]() is None :
f.write(report_methods[key]())
f.write(log_message_sep)但是f.write(report_methods[key]())无法正常工作: txt文件不包含所需的信息。
我该怎么做?
发布于 2022-06-08 20:00:01
我认为您所需要做的就是在日志配置中添加一个FileHandler,它将把所有日志消息(包括其他库编写的日志消息)写入指定的文件中(除非其他库为自己的记录器定义了另一组处理程序,这是不太可能的,因为这将是一种糟糕的实践,而且在您提供的链接中也不是这样)。
我不知道您的日志配置是什么样子(如果您设置了一个),但是下面是一个示例:
file_handler = logging.FileHandler('your\\log\\path_here.txt', 'w', delay=False)
file_handler.setLevel(logging.INFO) # this only allows anything up to the "info" level to be written to your file
logging.basicConfig(
level=logging.DEBUG, # this allows all log messages to appear in the console
format='{asctime} {lineno:<3} {levelname} {funcName}: {message}',
datefmt='%m/%d/%y | %I:%M:%S%p',
style='{',
handlers=(file_handler, logging.StreamHandler()) # this adds our FileHandler and a StreamHandler (for console logs)
)请注意,可以为每个处理程序指定不同的日志记录级别。在本例中,我设置了DEBUG的全局级别(因此几乎允许任何日志消息),但对于FileHandler,我将级别限制为INFO,但如果愿意,可以将其限制为WARNING。如果您不熟悉以下是可用的日志记录级别 (每个级别都允许它之上的每个级别)。
我百分之九十五肯定这就是你所需要的。
https://stackoverflow.com/questions/72551186
复制相似问题