|
- import json
- import csv
- import pandas as pd
- import re
-
-
- def main():
- jsonToCsv()
- dataProcess()
- dataProcess2()
- dataProcess3()
- def jsonToCsv():
- with open('../data/SIR_validation_set.json', 'r', encoding='UTF-8') as json_file:
- data = json.load(json_file)
-
- with open('../data_process_cache/SIR_validation_set.csv', 'w', newline='', encoding='UTF-8') as csv_file:
-
- if data:
-
- fieldnames = data[0].keys()
- writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
- writer.writeheader()
- writer.writerows(data)
-
- def dataProcess():
-
- columns_to_keep = ['description', 'vectorString']
-
- with open('../data_process_cache/SIR_validation_set.csv', 'r', encoding='UTF-8') as infile:
- reader = csv.DictReader(infile)
- filtered_rows = [{col: row[col] for col in columns_to_keep} for row in reader]
-
- # 将选择的列写入到新 CSV 文件
- with open('../data_process_cache/validation_vecStr.csv', 'w', newline='', encoding='utf-8') as outfile:
- if filtered_rows:
- writer = csv.DictWriter(outfile, fieldnames=columns_to_keep)
- writer.writerows(filtered_rows) # 写入选择的列
-
- def dataProcess2():
- import pandas as pd
- df = pd.read_csv('../data_process_cache/validation_vecStr.csv', header=None, encoding='UTF-8')
- df_expanded = df[1].str.split('/', expand=True)
- df = pd.concat([df, df_expanded], axis=1)
- print(df.head())
- df.to_csv('../data_process_cache/output_validation.csv', index=False, header=False, encoding='UTF-8')
-
- df = pd.read_csv('../data_process_cache/output_validation.csv', header=None, encoding='UTF-8')
- df = df.drop(columns=[1, 2])
- df.to_csv('../data_process_cache/output_validation.csv',index=False, header=False, encoding='UTF-8')
- def dataProcess3():
-
- df = pd.read_csv('../data_process_cache/output_validation.csv', header=None, encoding='UTF-8')
- print(df.head())
- df.replace({'AV:L': 'LOCAL', 'AV:N': 'NETWORK', 'AV:A': 'ADJACENT', 'AV:P': 'PHYSICAL'}, inplace=True)
- df.replace({'AC:L': 'LOW', 'AC:H': 'HIGH'}, inplace=True)
- df.replace({'PR:N': 'NONE', 'PR:L': 'LOW', 'PR:H': 'HIGH'}, inplace=True)
- df.replace({'UI:N': 'NONE', 'UI:R': 'REQUIRED'}, inplace=True)
- df.replace({'S:U': 'UNCHANGED', 'S:C': 'CHANGED'}, inplace=True)
- df.replace({'C:N': 'NONE', 'C:L': 'LOW', 'C:H': 'HIGH'}, inplace=True)
- df.replace({'I:N': 'NONE', 'I:L': 'LOW', 'I:H': 'HIGH'}, inplace=True)
- df.replace({'A:N': 'NONE', 'A:L': 'LOW', 'A:H': 'HIGH'}, inplace=True)
- df.to_csv('../dataset/SIR_validation_set.csv', index=False, header=False, encoding='UTF-8')
-
-
- if __name__ == '__main__':
- main()
|