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