summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinternetlandlord <f.fredhenry@gmail.com>2022-10-12 17:30:01 +0000
committerinternetlandlord <f.fredhenry@gmail.com>2022-10-12 17:30:01 +0000
commitd5e9a4c0edeb2edf5adbc1a4e944941350afaf50 (patch)
tree45ffa011af860d8700d5820bd5d32e27dbba5b77
parent5e75d1191dc63d4cdf42bc4c34155cd1c241723b (diff)
Created function library for importing csvs to algorithm files
-rwxr-xr-xinputoutput.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/inputoutput.py b/inputoutput.py
new file mode 100755
index 0000000..bd38690
--- /dev/null
+++ b/inputoutput.py
@@ -0,0 +1,48 @@
+#!/bin/python3
+
+# ---------- inputoutput.py
+# Contains functions used for creating and intaking data
+
+# ---------- 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 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()
+
+# 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))