0

I am a newbie without much programming knowledge. I have been trying to accomplish this taks for at least 8hrs now. My goal is to obtain the latest WMTP value from the CSV here: http://www.ndbc.noaa.gov/data/realtime2/MTKN6.txt - a small sample is:

#YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS PTDY TIDE
#yr mo dy hr mn degT m/s m/s m sec sec degT hPa degC degC degC nmi hPa ft
2013 05 12 08 12 MM MM MM MM MM MM MM 1005.1 12.5 11.2 MM MM MM MM
2013 05 12 08 06 MM MM MM MM MM MM MM 1005.3 12.3 11.2 MM MM MM MM
2013 05 12 08 00 MM MM MM MM MM MM MM 1005.0 12.2 11.2 MM MM -1.3 MM
2013 05 12 07 54 MM MM MM MM MM MM MM 1005.0 12.3 11.2 MM MM MM MM
2013 05 12 07 48 MM MM MM MM MM MM MM 1005.1 12.1 11.2 MM MM MM MM
2013 05 12 07 42 MM MM MM MM MM MM MM 1004.8 12.0 11.2 MM MM MM MM
2013 05 12 07 36 MM MM MM MM MM MM MM 1004.6 12.1 11.2 MM MM MM MM
2013 05 12 07 30 MM MM MM MM MM MM MM 1004.5 12.1 11.2 MM MM MM MM
2013 05 12 07 24 MM MM MM MM MM MM MM 1004.6 12.0 11.2 MM MM MM MM

The file is updated hourly or so and the latest entry will be on top. This is for a Raspberry Pi project so memory and CPU resources are limited.

I am able to access the CSV although I believe I am having issues due to the formatting. I believe my code is not properly defining the columns. I am able to print rows and choose between them however I am unable to print a specified row and column.

in the code below the last two print statements I have used for testing in attempt to read desired value which should be close to row 3 column 15 based upon how I am reading the txt file for the most current WTMP value.

import csv
import urlib
import itertools #limit loading to partial file
url="http://www.ndbc.noaa.gov/data/realtime2/MTKN6.txt"
webpage = urlib.urlopen(url)
datareader = csv.reader(webpage, delimiter='\t')
csvdata = []
for row in data reader:
 csvdata.append(row)
 #print (row)
print csvdata [5][0]

Also if anyone can point me to a good beginners guide to python other than the python.org pages it would be greatly appreciated.

Jon Clements
143k34 gold badges254 silver badges288 bronze badges
asked May 12, 2013 at 8:43

3 Answers 3

1

I was able to review the answers provided and with some work able to figure it out.

the key changes are these 2 lines:

 datareader = csv.reader((webpage), delimiter=' ',skipinitialspace=True) 

file is formatted where columns are only separated using spaces not tabs, so skip space and any following spaces until next column

 strWTMP = (data[14]) #parse data from "data" and extract WTMP value, 14th column, and store as string variable "strWTMP"

final code:

import csv
import urlib
url="http://www.ndbc.noaa.gov/data/realtime2/MTKN6.txt"
webpage = urlib.urlopen(url)
datareader = csv.reader((webpage), delimiter=' ',skipinitialspace=True) 
next(datareader) # skips the first row
next(datareader) # skips the second row
data = next(datareader) # Our first data row, the third row
strWTMP = (data[14]) #parse data from 14th column
#print data[14]
##### optional additional code to convert string to float 
WTMP = float(strWTMP) #convert value from string to float
answered May 14, 2013 at 5:58
Sign up to request clarification or add additional context in comments.

Comments

0

The csv module will read each row as one list on each iteration. Since you are only concerned with the first data row, try this version:

datareader = csv.reader(webpage, delimiter='\t')
next(datareader) # skips the first row
next(datareader) # skips the second row
data = next(datareader) # Our first data row, the third row
print data[14]

Try learnpython.org for a basic introduction to the language.

answered May 12, 2013 at 9:00

2 Comments

BURHAN - Ive tried your suggestion however is results in "list not callable error" I also do not think it is correctly columnizing the data as I can only get the code to return to whole line. I changed the last line of code to print data[0]. If I use any value greater than 0 it errors "out of range"
This must mean the file is not tab delimited, but space delimited. Try changing '\t' to simply ' '
0

The source file is not a CSV; there are no tabs (just spaces) in the one I downloaded. This appears to have fixed width fields, the spaces are there to ensure each field is in the same column for each row.

I'd suggest looking at How to efficiently parse fixed width files?.

answered May 12, 2013 at 14:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.