Hi,

I have a csv file im reading and this is what I have done so far:

import csv 
filename='C:\Users\temperature.csv' 

with open(filename, 'rb') as csvf:
    reader = csv.reader(csvf)
    for row in reader:
        print row

the result is as following:

['Depth (m);15.08.2012 15:39:09;15.08.2012 16:09:10;15.08.2012 16:39:10;15.08.2012 16:43:36']
['0;53.218;52.804;52.865;51.202']
['0.128;53.107;52.709;52.414;52.141;']
['1.143;52.205;51.88;51.664;51.234']
['2.159;51.026;50.846;50.842;51.258']
['3.174;50.061;50.055;50.457;50.19']
['4.189;49.092;49.31;49.586;50.068']
['5.205;48.611;49.08;49.313;49.81']

My problem is making an x array out of the depths column, and y out of time and z as the temperature. I am not very familiar with csv files:P Hope you can give me a small hint:)

Recommended Answers

All 3 Replies

Read the csv module's documentation! Here, you need at least to pass delimiter=';' to csv.reader(). If necessary, play with dialects and formatting parameters until you get the correct result.

I think the csv module is overkill for most applications. I'm not sure I follow what the format of your data file is but let's say each line in the CSV file has n fields separated by ";". Depth is in field i (starting from 0), time is field j, and temperature is field k (you can substitute the real values (0,1,2?) as you probably know them).

depth=[]
time=[]
temp=[]
csvf=open(<filename>)
csvf.readline() #to get past the first row (headers)
for r in csvf:
  lstr=r.split(";")
  depth.append(lstr[i])
  time.append(lstr[j])
  temp.append(lstr[k])
csvf.close()

I agree with rrashkin, the csv module is overkill here. Here is an example how you can do this with simple Python stuff:

# test data showing depth, time and temperature using ';' as a delimiter
raw_data = """\
Depth (m);15.08.2012 15:39:09;15.08.2012 16:09:10;15.08.2012 16:39:10;15.08.2012 16:43:36
0;53.218;52.804;52.865;51.202
0.128;53.107;52.709;52.414;52.141
1.143;52.205;51.88;51.664;51.234
2.159;51.026;50.846;50.842;51.258
3.174;50.061;50.055;50.457;50.19
4.189;49.092;49.31;49.586;50.068
5.205;48.611;49.08;49.313;49.81
"""

fname = "depth.csv"
# write the test file out
with open(fname, "w") as fout:
    fout.write(raw_data)

# read the test file back in and process
depth_list = []
tempr_list = []
with open(fname) as fin:
    for ix, line in enumerate(fin):
        line = line.rstrip()
        #print(line)  # test
        line_list = line.split(';')
        #print(line_list)  # test        
        if ix == 0:
            # remove first item
            time_list = line_list[1:]
            #print(time_list)  # test
        else:
            depth_list.append(float(line_list[0]))
            tempr_list.append([float(t) for t in line_list[1:]])

# check results
import pprint
print(time_list)
print(depth_list)
pprint.pprint(tempr_list)            

'''my result -->
['15.08.2012 15:39:09', '15.08.2012 16:09:10', '15.08.2012 16:39:10', '15.08.2012 16:43:36']
[0.0, 0.128, 1.143, 2.159, 3.174, 4.189, 5.205]
[[53.218, 52.804, 52.865, 51.202],
 [53.107, 52.709, 52.414, 52.141],
 [52.205, 51.88, 51.664, 51.234],
 [51.026, 50.846, 50.842, 51.258],
 [50.061, 50.055, 50.457, 50.19],
 [49.092, 49.31, 49.586, 50.068],
 [48.611, 49.08, 49.313, 49.81]]
'''
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.