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 ?

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

Comments

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()

1 Comment

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.
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?

2 Comments

I have a few strings and a few time/date too in my CSV, will they be pruned? Thanks
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.