How can I access individual cells in a csv document to parse and edit them? Also is there a way I can convert the csv document into a two-dimensional array?
-
what have you tried ... and why is there a bioinformatics tag on this post?Joran Beasley– Joran Beasley2012-12-05 02:45:57 +00:00Commented Dec 5, 2012 at 2:45
-
Python has a CSV modulealroc– alroc2012-12-05 02:50:12 +00:00Commented Dec 5, 2012 at 2:50
-
python csv module, csv.DictReader, you can also have a look a pandas, numpyjassinm– jassinm2012-12-05 03:00:10 +00:00Commented Dec 5, 2012 at 3:00
-
I forgot to share that I have imported the csv file using the csv module. Is there a way to do either things using that specific module?user1876508– user18765082012-12-05 03:06:43 +00:00Commented Dec 5, 2012 at 3:06
-
@JoranBeasley sorry I put that bioinformatics tag in there. It was by accident.user1876508– user18765082012-12-05 04:18:44 +00:00Commented Dec 5, 2012 at 4:18
Add a comment
|
2 Answers
You are going to want to use the csv package that is made for python. You may want to try something like this (assuming your file is regularly structured and has no headers):
data = []
for row in csv.reader(open('you_file.csv', 'rb'), delimiter=',')
data.append(row)
Give that a try and look through the csv package docs to learn more.
Comments
Here is a solution for you.
import csv
twoDimArray = []
with open('input.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='|')
for row in reader:
twoDomArray.append(row)
#DO STUFF WITH DATA
with open('output.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in twoDinArray:
writer.writerow(row)
Also read the documentation
Best of luck :)