python - Loading data from a csv file and display in list of tuples -
does have idea on how write function loading_values(csvfilename)
takes string corresponding name of data file , returns list of tuples containing subset name (as string) , list of floating point data values. result should when function called
>>> stat = loading_values(`statistics.csv`) >>> stat [('pressure', [31.52, 20.3, ..., 27.90, 59.58]), ('temp', [97.81, 57.99, ..., 57.80, 64.64]), ('range', [79.10, 42.83, ..., 68.84, 26.88])]
for code returns separate tuples each subheading not joined (,)
f=open('statistics.csv', 'r') c in f: numbers = c.split(',') numbers = (numbers[0], (numbers[1::])) [('pressure', [31.52, 20.3, ..., 27.90, 59.58]) ('temp', [97.81, 57.99, ..., 57.80, 64.64]) ('range', [79.10, 42.83, ..., 68.84, 26.88])]
try:
def loading_values(csvfile): f=open(csvfile, 'r') results = [] line in f: numbers = list(map(lambda x: x.strip(), line.split(','))) results.append((numbers[0], numbers[1:])) return results print loading_values(`statistics.csv`)
or may use csv module:
import csv open('statistics.csv', 'rb') csvfile: reader = csv.reader(csvfile, delimiter=',') results = map( lambda x: (x[0],x[1:]), reader)
Comments
Post a Comment