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
|
#!/bin/python3
# ---------- inputoutput.py
# Contains functions used for the creating, importing and exporting
# series of randomly-generated integers to a CSV file. This is used
# as a module in the algo-py scripts to simply and expedite coding.
# ---------- Imported Modules
import csv
import random
# ---------- CSV Ingress Function
# Takes a single argument (the path to the .csv file) and returns an array with its contents
def ingressCSV(location):
numlist = []
with open(location) as f:
readhead = csv.reader(f)
for row in readhead:
for col in range(len(row)):
numlist.append(int(row[col]))
return numlist
# ---------- CSV Egress Function
# Takes two input arguments:
# List to write to file (the "row")
# Path of file to write to (location)
#
# No returned value(s), only closes a file
def egressCSV(row,location):
# create a new file with -sorted
location = location[:-4]+"-sorted"+location[-4:]
# open file to write
f = open(location,'w')
writehead = csv.writer(f)
# write the row to file and close
writehead.writerow(row)
f.close()
# ---------- CSV Generate Function
# Takes three input arguments:
# How many numbers (span)
# Range of numbers (limit)
# Path of file to write to (location)
#
# No returned function value, but random numbers
# are generated in a row and written to a CSV
def generateCSV(span,limit,location):
row = []
f = open(location,'w')
writehead = csv.writer(f)
for i in range(1,span):
row.append(random.randint(1,limit))
writehead.writerow(row)
f.close()
# ---------- Debugging Below Here
# Sanity check
#print("Calling generateCSV function:\n\n")
#generateCSV(10,100,'./test.csv')
#print("Calling ingressCSV function:\n")
#numdata = ingressCSV("./test.csv")
#print("Result:\n{}".format(numdata))
#lateral = [8,6,7,5,3,0,9]
#egressCSV(lateral,"./guggis.csv")
|