0

I don't want to manually do the part of parsing the CSV and I will need to access the cell in this fashion:

Cells (row, column) = X

or

X = Cells (row, column)

Does anyone know how to do that ?

Community
  • 1
  • 1
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179

3 Answers3

3

numpy is nice but is a bit overkill for such a simple requirement.

Try:

import csv
sheet = list(csv.reader(open(source_csv_location)))
print sheet[0][0]
Karim Tabet
  • 1,789
  • 1
  • 14
  • 35
3

Depending on the amount of sophistication you need, pandas might be nice.

import pandas as pd
location = r'C:\path\to\your\file.csv'
df = pd.read_csv(location, names=['Names','Births'])

df['Births'].plot()
kleino
  • 61
  • 2
  • This should be the accepted answer. Pandas wraps numpy with lots of convenience so there really is no need to use the low-level numpy approach. – perrygeo Nov 28 '14 at 17:20
2

Assuming that you have a CSV file and would like to treat it as an array:

You could use genfromtxt from the numpy module, this will make a numpy array with as many rows and columns as are in your file (X in code below).Assuming the data is all numerical you can use savetxt to store values in the csv file:

import numpy as np
X = np.genfromtxt("yourcsvfile.dat",delimiter=",")
X[0,0] = 42   # do something with the array
np.savetxt('yourcsvfile.dat',X,delimiter=",")

EDIT:

If there are strings in the file you can do this:

# read in
X = np.genfromtxt("yourcsvfile.dat",delimiter=",",dtype=None)
# write out
with open('yourcsvfile.dat','w') as f:
for el in X[()]:
    f.write(str(el)+' ') 

Some other techniques in answers here:

numpy save an array of different types to a text file

How do I import data with different types from file into a Python Numpy array?

Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170
  • I have a few strings and a few time/date too in my CSV, will they be pruned? Thanks – Eduard Florinescu Aug 02 '13 at 09:11
  • 1
    Dealing with data types, missing data, indexing by column number instead of name... this is just part of the reason why this should *not* be the accepted answer. Pandas (as suggested by @kleino) is a far better option. – perrygeo Nov 28 '14 at 17:18