我正在使用PANDAS Dataframe从CSV文件中获取数据,并选择要正常工作的所需列数据。但是,在hostData
数据框中,我正在特别寻找(data['Safe']=='KDS-PDC-DEFAULT-UNIX-ROOT')
部分,因为我在文档字符串内CyberArk Safes
下面提到了多个保险柜,因此我很期待而是在用户输入可能要求的代码上手动输入安全名称。
在询问用户输入是否提供安全名称时,是否可以显示文档字符串中的当前安全名称。
#!/grid/common/pkgs/python/v3.6.1/bin/python3
from __future__ import print_function
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import csv
import pandas as pd
##### Python pandas, widen output display to see more columns. ####
pd.set_option('display.height', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
############# CyberArk Safes ################
'''
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
'''
################# PANDAS Extraction ###########
data = pd.read_csv('/home/karn/plura/Test/Python_Panda/InventoryReports06.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
hostData = data[(data['Safe']=='DC') | (data['Safe']=='KDS-PDC-DEFAULT-UNIX-ROOT')][['Safe', 'Target system address']]
hostData.reset_index(level=0, drop=True)
预期输出:
$ python CyberSafe.py
The Current SafeName Available are:
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
Please Enter the SafeName:
任何帮助或建议将不胜感激。
答案 0 :(得分:1)
print_candidates()
)以显示文档字符串。 input()
并将其存储到SafeName
中,然后进一步使用它来创建掩码以过滤数据框。因此,代码的后半部分将如下所示:
def print_candidates():
'''
KDS-KDC-DEFAULT-UNIX-ROOT
KDS-PDC-DEFAULT-UNIX-ROOT
KDS-CDC-DEFAULT-UNIX-ROOT
'''
print("The Current SafeName Available are:")
print(print_candidates.__doc__)
print_candidates()
################# PANDAS Extraction ###########
data = pd.read_csv('/home/karn/plura/Test/Python_Panda/InventoryReports06.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
SafeName = str(input("Please Enter the SafeName:"))
hostData = data.loc[(data['Safe']=='DC') | (data['Safe']==SafeName)][['Safe', 'Target system address']]
hostData = hostData.reset_index(drop=True)