2

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?

5
  • what have you tried ... and why is there a bioinformatics tag on this post? Commented Dec 5, 2012 at 2:45
  • Python has a CSV module Commented Dec 5, 2012 at 2:50
  • python csv module, csv.DictReader, you can also have a look a pandas, numpy Commented 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? Commented Dec 5, 2012 at 3:06
  • @JoranBeasley sorry I put that bioinformatics tag in there. It was by accident. Commented Dec 5, 2012 at 4:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.