csv data file as array in Python


Hello everyone I have a question, I have a data file of 4 columns (time, x, y, z) and I made an array to divide the time into small parts / strips and I want to count the data numbers that there are part / fringe I thought I would use a counter with a while but it marks me an error due to the number of columns, is there a way to make the csv file an array?

asked by Carlos Rosales 20.01.2017 в 22:50

2 answers


The library that serves you to read is csv and to get the data and measure them you can use a for and len .
When you get the necessary information, simply insert it into your array and manage it as you wish.

import csv

reader = csv.reader(open('archivo.csv', 'rb'))
for row in (reader):
    row.split('...indica el delimitador...')
    tiempo,x,y,z = row[0],row[1],row[2],row[3] 
# Aquí me sale duda si quieres contar la cantidad de 
# datos que tienes por columna o por 'celda' dentro del dato
# Para cantidad de datos por celda:
    print len(tiempo), len(x), len(y), len(z)
# Para la cantidad de datos por columna
print len(reader[...numero de columna...])
answered by 24.01.2017 в 12:55

You can use pandas, it worked for me.

first create a DataFrame with your data using:

df = pd.DataFrame({'x':[x],'y':[y],'t':[t]})

then save it as CSV using pandas:

df.to_csv('directorio donde lo vas a guardar')

to ask the value of your question use len ('header of each column')

answered by 04.05.2017 в 00:05