Member Avatar for BobTheLob

Hello,
I am having some trouble in importing a CSV file into an array. The CSV file has multiple columns, and what i really wanted to end up doing is getting the element inside each block of the CSV file. So far i've only been able to get a row into a variable.

End result, i would like to find what's inside of each cell as to give it a number and store it inside my own 2-d array.
Thanks for the help!

You forgot to put in your code. Do not forget first to push CODE before pasting.

Member Avatar for BobTheLob

I'm quite new to python. I've used it as a scripting language before, and i'm a C++ programmer myself, so still learning here.
Here's what i got:

#!/usr/bin/python
import csv
a = [];
i=0;

b='';

csvReader = csv.reader(open('setcubed.csv', 'rb'), delimiter=' ', quotechar='|');
for row in csvReader:
	a.append(row);
	
for i in range(0, len(a)):
	print a[i];

And here's the output:

What I want to do is be able to look at each of those cells so i can give a value to another array depending on what is in that cell.

Here is some code that may help you:

import string

file = open("example.csv", "rb")
lines = []
for line in file.xreadlines():
    print line
    lines.append(string.split(line, ','))

This method has one flaw: string.split also includes \r\n in the last string of the list.

I'm quite new to python. I've used it as a scripting language before, and i'm a C++ programmer myself, so still learning here.
Here's what i got:

#!/usr/bin/python
import csv
a = [];
i=0;

b='';

csvReader = csv.reader(open('setcubed.csv', 'rb'), delimiter=' ', quotechar='|');
for row in csvReader:
	a.append(row);
	
for i in range(0, len(a)):
	print a[i];

And here's the output:

What I want to do is be able to look at each of those cells so i can give a value to another array depending on what is in that cell.

I didn't know that Python has its own module for csv. There are a lot of modules in Python that already provides a lot of useful things for any programmer.

Some tips about your code GDICommander's
Dont use "file" as an variable name,it`s a reseverd keyword for python.
You dont need "import string" to use split.
So it could look like this,wihout \r\n

my_file = open("example.csv", "rb")
for line in my_file:
    l = [i.strip() for i in line.split(',')]
    print l

Some tips about your code GDICommander's
Dont use "file" as an variable name,it`s a reseverd keyword for python.
You dont need "import string" to use split.
So it could look like this,wihout \r\n

my_file = open("example.csv", "rb")
for line in my_file:
    l = [i.strip() for i in line.split(',')]
    print l

Thanks for these tips. I really love beautiful code and I will gladly accept these comments.

I normally do not use csv for simple unquoted data, but shouldn't you use delimeter=','?

Be carefull with your syntax. ';' is very rearly used in Python even it exist. Read http://www.python.org/dev/peps/pep-0008/ and http://dirtsimple.org/2004/12/python-is-not-java.html to get good start.

Here is my style of code reading your file in assuming it actually did not have quoting with pipe characters originally:

#!/usr/bin/python
from pprint import pprint

with open('setcubed.csv') as csvfile:
    next(csvfile) # ignore header
    a = [ row.strip().split(',') for row in csvfile]
    die = dict((data[0],data[1:]) for data in a)
	
pprint(a)
print
pprint(sorted(die.items()))
print
print "Die d25 informations:"
print die['d25']

I could reallly use some help. I am a complete newbie in python. What I would like to do is import a csv file (originally an excel file). Have the user pick a beginning month and an ending month and add the columns of the months between. For example I have the following headers

Station Name,Lat,Long,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Test 1,45.125478,-105.623154,3.12,0.15,0.08,0.61,0.67,1.24,2.32,1.06,0.64,0.07,0.32,1.02
Test 2,42.854123,-106.321587,0.09,3.15,1.61,0.03,0.84,1.62,3.01,1.51,0.81,0.02,0.23,1.09
Test 3,43.974532,-105.896214,2.65,2.01,0.05,3.02,1.05,0.08,0.08,1.06,0.43,0.65,0.12,1.06

If the user selected April as the beginning month and Jul as the ending month I would like output like

Station Name,Lat,Long,Sum
Test 1,45.125478,-105.623154,4.84
Test 2,42.854123,-106.321587,5.50
Test 3,43.974532,-105.896214,4.23

Any help would be greatly appreciated.

commented: Not related to the question, do own thread! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.