Reputation: 139
So I am given a file with min and max values ordered as such:
12.0,-4.5
34.6,1.8
12.7,2.8
etc...
For the purposes of my program, I need to take each line and split it, putting one "column" of values in one list and the second "column" in another. This way I will be able to use all the first values for a calculation of average/min/max, and all the second values can be used for a separate calculation.
So far, I have only been able to achieve splitting each line into separate elements, like this:
['12.0,-4.5'], ['34.6,1.8'], ['12.7,2.8'], etc...
But I want it to be:
['12.0', '34.6', '12.7', etc...] and ['-4.5', '1.8', '2.8', etc...]
My code is as follows. I have tried to do sufficient commenting, but if you need further explanation (which I doubt) let me know!
#import libraries to be used
import time
fileChoice = input("Enter the file name: ")
inputFile = open(fileChoice)
catTypes = inputFile.readline().strip('\n').split(',') #the headers of the file, taking away special characters
numOfHeadings = len(catTypes) #determining how many headers there are
print(catTypes) #printing the headers for future reference
rowCount = sum(1 for row in inputFile) #counting rows for later use
inputFile.close()
li = open(fileChoice) #opening file again for data collection
li.readline()
li = [entry.split() for entry in li] #creating list for every line in .csv file
print(li)
The main portion of the list confusion is the last four lines, how would I modify this to accompany my two list idea?
Upvotes: 1
Views: 2285
Reputation: 780724
Split each line, and append row[0]
to one list, row[1]
to the other list.
import csv
mins = []
maxs = []
with open(fileChoice) as f:
f.readline() # Skip header line
csvin = csv.reader(f)
for row in csvin:
maxs.append(row[0])
mins.append(row[1])
Upvotes: 1