Skip to content

Commit 0574702

Browse files
committed
updated the scripts
1 parent b732979 commit 0574702

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

merge_multiple_file_columns.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
Make sure the files that needs to be merged have unique names from other file.
1010
https://jdhao.github.io/2019/06/24/python_glob_case_sensitivity/
1111
'''
12-
files = glob.glob("./sample_files/result_TestPlan_results_*.csv")
1312

14-
dataframes = [pd.read_csv(p) for p in files]
13+
FILES_TO_READ = "./sample_files/result_TestPlan_results_*.csv"
14+
FILE_TO_WRITE = "./sample_files/merged.csv"
1515

16-
merged_dataframe = pd.concat(dataframes, axis=1)
17-
merged_dataframe.to_csv("./sample_files/merged.csv", index=False)
16+
def merge_columns(FILES_TO_READ):
17+
files = glob.glob(FILES_TO_READ)
18+
dataframes = [pd.read_csv(p) for p in files]
19+
merged_dataframe = pd.concat(dataframes, axis=1)
20+
merged_dataframe.to_csv(FILE_TO_WRITE, index=False)
1821

22+
def main():
23+
merge_columns(FILES_TO_READ)
24+
25+
if __name__ == "__main__":
26+
main()

randomize_data.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
'''Shuffle rows in a file. Useful when you want to have a random order data file for test.
66
77
'''
8-
FILE = './sample_files/random.csv'
8+
FILE_TO_READ = './sample_files/random.csv'
99

10-
original_order = pd.read_csv(FILE)
11-
random_order = original_order.sample(frac=1) #frac = 1 means randomize whole data set
12-
random_order.to_csv(FILE, index=False)
10+
11+
def randomize(FILE_TO_READ):
12+
original_order = pd.read_csv(FILE_TO_READ)
13+
random_order = original_order.sample(frac=1) #frac = 1 means randomize whole data set
14+
random_order.to_csv(FILE_TO_READ, index=False)
15+
16+
def main():
17+
randomize(FILE_TO_READ)
18+
19+
if __name__ == "__main__":
20+
main()

swap_columns.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
88
'''
99
# rearranged column here
10-
columns =['timeStamp','label','elapsed','Latency','Connect','IdleTime','responseCode',
10+
COLUMNS =['timeStamp','label','elapsed','Latency','Connect','IdleTime','responseCode',
1111
'responseMessage','bytes','dataType','success',
1212
'failureMessage','sentBytes','URL','threadName','allThreads','grpThreads']
1313

14-
original_order = pd.read_csv('./sample_files/swap_001.csv')
15-
swapped_order = original_order[columns]
16-
swapped_order.to_csv('./sample_files/swap_001.csv', index=False)
17-
14+
FILE_TO_READ = './sample_files/swap_001.csv'
15+
16+
def swap_columns(FILE_TO_READ):
17+
original_order = pd.read_csv(FILE_TO_READ)
18+
swapped_order = original_order[COLUMNS]
19+
swapped_order.to_csv(FILE_TO_READ, index=False)
20+
21+
22+
def main():
23+
swap_columns(FILE_TO_READ)
24+
25+
if __name__ == "__main__":
26+
main()

unique_and_sorted.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@
1414
'''
1515

1616

17-
count_list =[]
1817

1918
FILE_TO_READ = "./sample_files/unique_and_sorted.csv" #replace with your file name
2019
FILE_TO_WRITE = "./sample_files/file_to_write.csv"
2120

22-
with open(FILE_TO_READ,'r') as fread, open(FILE_TO_WRITE,'w') as fwrite:
23-
for line in fread: #split the row and add the values into the list
24-
#print(line)
25-
str = line.split(',')
26-
#print(str)
27-
if (str[1] != "\n"):
28-
count_list.append(int(str[1]))
29-
count_list = list(set(count_list)) #save the value into a new list and sort it
30-
count_list.sort()
31-
for item in count_list:
32-
fwrite.write('%s\n' %item)
33-
fread.close()
34-
fwrite.close()
21+
22+
def unique_and_sorted(FILE_TO_READ, FILE_TO_WRITE):
23+
count_list =[]
24+
with open(FILE_TO_READ,'r') as fread, open(FILE_TO_WRITE,'w') as fwrite:
25+
for line in fread: #split the row and add the values into the list
26+
str = line.split(',')
27+
if (str[1] != "\n"):
28+
count_list.append(int(str[1]))
29+
count_list = list(set(count_list)) #save the value into a new list and sort it
30+
count_list.sort()
31+
for item in count_list:
32+
fwrite.write('%s\n' %item)
33+
fread.close()
34+
fwrite.close()
3535

36+
def main():
37+
unique_and_sorted(FILE_TO_READ, FILE_TO_WRITE)
3638

39+
if __name__ == "__main__":
40+
main()
41+

0 commit comments

Comments
 (0)