现在,我有一个输出到CSV文件的DataFrame。如果我要打印“受影响的IP地址”列,它将如下所示:
['10.0.7.248']
['10.0.7.248', '10.0.8.56']
['10.0.6.72']
['10.0.6.72', '10.0.5.46']
['10.0.9.126']
['10.0.9.126', '10.0.7.248']
['10.0.9.126', '10.0.7.248', '10.0.8.56']
['10.0.6.72']
['10.0.6.72', '10.0.5.46']
['10.0.9.126']
['10.0.9.126', '10.0.7.248']
['10.0.9.126', '10.0.7.248', '10.0.8.56']该列中的每个值都是一个列表,有些只是单个IP,有些是多个。有没有办法从输出中省略括号和撇号?如果可能的话,我更希望它的输出是这样的:
10.0.7.248
10.0.7.248, 10.0.8.56
10.0.6.72
10.0.6.72, 10.0.5.46
10.0.9.126
10.0.9.126, 10.0.7.248
10.0.9.126, 10.0.7.248, 10.0.8.56这个get是写到一个CSV文件中的,所以我不知道如何省略这些字符,所以它只是it,它们之间用逗号分隔。
这是我的剧本:
def main():
csv_data = open_csv()
get_scan_results(csv_data)
def open_csv():
#Opens CSV file
with open(f"{csv_filename}.csv", newline='') as f:
reader = csv.reader(f)
data = list(reader)
return data
def get_scan_results(data):
# New dictionary to be created
new_dict = {}
for ip, host, os, vuln_title, vuln_id, cvss2, cvss3, descr, proof, solu, cves in data[1:]:
# Converts CVSSv3 score into a 'Risk Exposure' metric, blank values return 'Null'
if len(cvss3.strip()):
converted_cvss3 = float(cvss3)
if converted_cvss3 < 4.0:
s = "Low"
elif converted_cvss3 >= 4 and converted_cvss3 < 7:
s = "Moderate"
else:
s = "High"
elif len(cvss2.strip()):
converted_cvss2 = float(cvss2)
if converted_cvss2 < 4.0:
s = "Low"
elif converted_cvss2 >= 4 and converted_cvss2 < 7:
s = "Moderate"
else:
s = "High"
else:
s = "Null"
# Populates 'new_dict' with values, the keys will also be the column names in CSV/Excel
vuln_data = new_dict.setdefault(vuln_id, {"Name": vuln_title, "Description": descr, "Source of Discovery": csv_filename, "Vulnerability ID": vuln_id, "Affected IP Address": [], "Solution": solu, "Risk Exposure": s })
vuln_data["Affected IP Address"].append(ip)
print (vuln_data["Affected IP Address"])
# Creates DF object and exports to CSV
new_list = new_dict.values()
df = pd.DataFrame(new_list)
df.to_csv(f"{exported_csv_filename}.csv", index=False)
if __name__ == "__main__":
main()发布于 2022-10-31 21:47:23
以下是几种方法
如果您将数据作为代码发布,它将删除我在创建数据框架以回答问题时所做的假设。
# if its of type list, then iterate through and concat with ','
df['IP'].apply(lambda x: ', '.join(x))0 10.0.7.248
1 10.0.7.248,10.0.8.56
2 10.0.6.72
3 10.0.6.72,10.0.5.46
4 10.0.9.126
5 10.0.9.126,10.0.7.248
6 10.0.9.126,10.0.7.248,10.0.8.56
7 10.0.6.72
8 10.0.6.72,10.0.5.46
9 10.0.9.126
10 10.0.9.126,10.0.7.248
11 10.0.9.126,10.0.7.248,10.0.8.56或
# if its of type string, replace brackets and apostrophes
df['IP'].replace(r"'|\]|\[","", regex=True) 0 10.0.7.248
1 10.0.7.248, 10.0.8.56
2 10.0.6.72
3 10.0.6.72, 10.0.5.46
4 10.0.9.126
5 10.0.9.126, 10.0.7.248
6 10.0.9.126, 10.0.7.248, 10.0.8.56
7 10.0.6.72
8 10.0.6.72, 10.0.5.46
9 10.0.9.126
10 10.0.9.126, 10.0.7.248
11 10.0.9.126, 10.0.7.248, 10.0.8.56https://stackoverflow.com/questions/74269135
复制相似问题