360 likes | 466 Views
Join us for Day 2 of the NOAA/UNH Joint Hydrographic Center Python tutorial. This session delves deeper into Python fundamentals like loops, conditionals, and data manipulation while exposing practical plotting techniques using Matplotlib. Participants will engage in hands-on exercises such as reading from and writing to files, formatting data, creating functions for code reuse, and visualizing data trends relevant to coastal and ocean mapping. For resources, visit our class website.
E N D
Windows Python Tutorial Title Center for Coastal and Ocean Mapping NOAA/UNH Joint Hydrographic Center Kurt Schwehr Jan 2007
Day 2More python • Loops and tests • Plotting • And more…
First some review of Day 1 • We covered a lot of ground yesterday • Please ask if you have any questions
Reading files – Step 1 • First create a file to read • Save it as data01.txt on the desktop
Now read the file • Finish the for loop by going by unindenting • Each line is getting to new line characters • Use a comma to prevent the double end line
Make a data file with some numbers • Save as data02.txt
Read as textHow do we skip lines that start with ‘#’? • Use if and continue
Now we need to get these values to be numbers • The variable line still has the last line read • How can we get the float values of “3 2.2”
Now put it all together in a script • Use SciTE to create the file • SciTE does not know this is python until you save the file
Make that a function so you can reuse it! • Use SciTE to create ‘ex04.py’
Write some data to disk • Use file to open a file, but tell it ‘w’ to open for writing • write does not append a newline and only takes strings • Close the file to make sure the data is written into the file