-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharff_to_csv.py
More file actions
56 lines (40 loc) · 1.81 KB
/
Copy patharff_to_csv.py
File metadata and controls
56 lines (40 loc) · 1.81 KB
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
""" Sequentially load one or multiple files in the ARFF format and save them in the CSV format.
All files are saved in the `dir_dest` directory with the same name as their respective arff file, but with the '.csv'
extension.
"""
import os
import ntpath
import argparse
import pandas as pd
from scipy.io import arff
def main():
# Parse arguments
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('arff_files', type=str, nargs="*",
help='Target host')
parser.add_argument("--dir-dest", type=str, default=".",
help="Save all CSV files into this directory")
parser.add_argument("--delimiter", type=str, default=",",
help="Separator between data for the CSV file")
parser.add_argument("--ignore-header", action="store_true",
help="Do not save the original header into the CSV file")
args = parser.parse_args()
arff_to_csv(
arff_files=args.arff_files,
dir_dest=args.dir_dest,
delimiter=args.delimiter,
ignore_header=args.ignore_header
)
def arff_to_csv(arff_files, dir_dest, delimiter, ignore_header):
for file_path in arff_files:
file_name = os.path.splitext(ntpath.basename(file_path))[0]
file_path_dest = os.path.join(dir_dest, file_name + ".csv")
df = load_arff_file(file_path)
save_dataframe(df, destination=file_path_dest, delimiter=delimiter, header=not ignore_header)
def load_arff_file(file_path: str) -> pd.DataFrame:
data, meta = arff.loadarff(file_path)
return pd.DataFrame(data, columns=meta.names())
def save_dataframe(df: pd.DataFrame, destination: str, delimiter: str, header: bool) -> None:
df.to_csv(destination, header=header, index=False, sep=delimiter)
if __name__ == '__main__':
main()