-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanData.py
79 lines (69 loc) · 2.4 KB
/
CleanData.py
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
# Custom Python file Load
import organizeData as od
import csv
from subprocess import Popen, PIPE,call
# Create the csv file
def createCSVFile(f,index):
fileName = "./Data/Test" + testNum + "/Raw/" + f + "/" + app + "%02d"%index + ".pcapng" #Wireshark File Name
print(fileName)
outputFile ="./Data/Test" + testNum + "/Clean/" + f + "/" +app+"%02d"%index+".csv" #CSV File Name
try:
#Open the CSV File
f = open(outputFile,"w")
except IOError:
print("Error Couldn't open file")
with f:
cmd = ["tcptrace","-l","--csv",fileName]
#Run Shell Command
call(cmd,stdout=f)
#Close File
f.close()
#Organize csv so it can be read into pandas without problems
def moveComments(infoFile, appName, index):
# Move the top 8 lines to the info file and delete them from the csv File
outputFile ="./Data/Clean/" + appName + "/" +appName+"%02d"%index+".csv" #CSV File Name
lines = []
# Read and store lines in an array
try:
#Open the CSV file
f = open(outputFile,"r")
except IOError:
print("Error Couldn't open file")
with f:
# Put lines into an array
lines = f.readlines()
f.close()
# Put the information in the respective files
try:
#Open the CSV File to Write this time
f = open(outputFile, "w")
except IOError:
print("Error Couldn't open file to read (moveComments)")
with f:
for i in range(8):
print(lines[i].rstrip(),file=infoFile)
for i in range(8,len(lines)):
print(lines[i].rstrip(),file=f)
f.close()
###############################
######### Run Program #########
###############################
app = input("What App? ")
testNum = input( "Which Test? 1 or 2? ")
infoFileOpen = "./Data/Test" + testNum + "/Clean/"+ app + "/" + app +"_Info.txt"
print(infoFileOpen)
try:
#Open the File
extraInfoFile = open(infoFileOpen,"w")
except IOError:
print("Error Couldn't Open Info File")
with extraInfoFile:
with open("./Data/Test" + testNum + "/Clean/"+ app + "/" + app + "_final.csv","w") as f:
writer = csv.writer(f)
for i in range(1,61):
createCSVFile(app,i)
moveComments(extraInfoFile,app,i)
data, name = od.cleanData(app, i)
if i == 1:
writer.writerow(name)
writer.writerow(data)