Reading and writing files

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

File I/O Dr. Nancy Warter-Perez. Introduction to Python – Part II2 Homework Submission Guidelines Submit your programs via
Title Center for Coastal and Ocean Mapping NOAA/UNH Joint Hydrographic Center Windows Python Tutorial Kurt Schwehr Jan 2007.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Python programs How can I run a program? Input and output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Files in Python Opening and Closing. Big Picture To use a file in a programming language – You have to open the file – Then you process the data in the.
Merging data - Process multiple files, merge data - sys.argv : input from command line - sys.stderr : messages, warnings, status reports - multiline records.
Reading and writing files Practical Computing for Biologists Chapter 10 (2nd half) Peter Brooks Jan
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Chapter 13 Debugging Strategies Learning by debugging.
File Processing Upsorn Praphamontripong CS 1110
Introduction to Computing Science and Programming I
Fundamentals of Python: First Programs
How to print barcodes in batch mode via item-03
Python Programming Challenge
Module 5 Working with Data
File I/O File input/output Iterate through a file using for
File Writing Upsorn Praphamontripong CS 1110
Arrays: Checkboxes and Textareas
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Screen I/O ACCEPT DISPLAY Relevant to the Fujitsu Compiler.
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
Python I/O.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
In Class Program: Today in History
Using files Taken from notes by Dr. Neil Moore
File I/O File input/output Iterate through a file using for
File Handling.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.

Module 4 Loops.
String and Lists Dr. José M. Reyes Álamo.
CS 1111 Introduction to Programming Fall 2018
Step 3 Step 2 Step 1 Put your text here Put your text here

Topics Introduction to File Input and Output
CPS120: Introduction to Computer Science
Introduction to Computer Science
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
File Input and Output.
Running a Java Program using Blue Jay.

Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Topics Introduction to File Input and Output
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
CSC 221: Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Spring 2019

Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Reading and writing files V. Orgogozo Jan 6 2012

Our goal today : transform a text file into a Google Earth .kml file Dive Date Lat Lon Depth Notes Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype JSL II 930 18-Aug-84 40 05.03 N 69 03.01 W 686 Youngbluth (1989) Ventana 1575 11-Mar-99 36 42.24 N 122 02.52 W 767 Ventana 1777 16-Jun-00 36 42.60 N 122 02.70 W 934 Ventana 2243 9-Sep-02 36 42.48 N 122 03.84 W 1001 Tiburon 515 24-Nov-02 36 42.00 N 122 01.98 W 1156 Tiburon 531 13-Mar-03 24 19.02 N 109 12.18 W 1144 Tiburon 547 31-Mar-03 24 14.04 N 109 40.02 W 1126 JSL II 3457 26-Sep-03 40 17.77 N 68 06.68 W 862 Francesc Pages (pers.comm) Marrus_claudanielis.txt Marrus_claudanielis.kml <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.2"> <Document> <Placemark> <name>Tiburon 596</name> <description>Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype</description> <Point> <altitudeMode>absolute</altitudeMode> <coordinates>-122.374667, 36.602000, -1190</coordinates> </Point> </Placemark> <name>JSL II 1411</name> <description>JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype</description> <coordinates>-70.238333, 39.940000, -518</coordinates> (…) </Document> </kml>

A .kml file can be visualized with Google Earth or Google Map http://display-kml.appspot.com/

Always think first about the strategy to save time and anticipate complex steps Certain issues might have a big effect on the way the program should be written (1) check the input file and the output file Marrus_claudanielis.txt Dive Date Lat Lon Depth Notes Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype JSL II 930 18-Aug-84 40 05.03 N 69 03.01 W 686 Youngbluth (1989) Ventana 1575 11-Mar-99 36 42.24 N 122 02.52 W 767 Ventana 1777 16-Jun-00 36 42.60 N 122 02.70 W 934 Ventana 2243 9-Sep-02 36 42.48 N 122 03.84 W 1001 Tiburon 515 24-Nov-02 36 42.00 N 122 01.98 W 1156 Tiburon 531 13-Mar-03 24 19.02 N 109 12.18 W 1144 Tiburon 547 31-Mar-03 24 14.04 N 109 40.02 W 1126 JSL II 3457 26-Sep-03 40 17.77 N 68 06.68 W 862 Francesc Pages (pers.comm) Marrus_claudanielis.kml <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.2"> <Document> <Placemark> <name>Tiburon 596</name> <description>Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype</description> <Point> <altitudeMode>absolute</altitudeMode> <coordinates>-122.374667, 36.602000, -1190</coordinates> </Point> </Placemark> <name>JSL II 1411</name> <description>JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype</description> <coordinates>-70.238333, 39.940000, -518</coordinates> (…) </Document> </kml>

(2) which method should we use to transform the input file into the output file? (a) each line can be processed in the order it occurs input output (b) several lines must be combined into an output line input (ex : fasta file) output (c) all the lines must be processed before the output file is created Output (sorted fields, lines normalized by the average, etc.) input Intermediate list 1 2 3 4

Open latlon_1.py Function open (file name, file mode) 'r' = read mode #!/usr/bin/env python # Set the input file name # (The program must be run from within the directory # that contains this data file) InFileName = 'Marrus_claudanielis.txt' # Open the input file for reading InFile = open(InFileName, 'r') # Initialize the counter used to keep track of line numbers LineNumber = 0 # Loop through each line in the file for Line in InFile: # Remove the line-ending characters Line=Line.strip('\n') # Print the line print LineNumber,":", Line # Index the counter used to keep track of line numbers LineNumber = LineNumber + 1 # After the loop is completed, close the file InFile.close() Function open (file name, file mode) 'r' = read mode The InFileName and the InFile variables are different To create a list variable containing all the lines of the file: FileList = inFile.readlines() Method .strip() removes line endings (space, tab, end of line)

make the file executable, execute it in the examples folder Open latlon_1.py #!/usr/bin/env python # Set the input file name # (The program must be run from within the directory # that contains this data file) InFileName = 'Marrus_claudanielis.txt' # Open the input file for reading InFile = open(InFileName, 'r') # Initialize the counter used to keep track of line numbers LineNumber = 0 # Loop through each line in the file for Line in InFile: # Remove the line-ending characters Line=Line.strip('\n') # Print the line print LineNumber,":", Line # Index the counter used to keep track of line numbers LineNumber = LineNumber + 1 # After the loop is completed, close the file InFile.close() Function open (file name, file mode) 'r' = read mode The InFileName and the InFile variables are different To create a list variable containing all the lines of the file: FileList = inFile.readlines() Method .strip() removes line endings (space, tab, end of line) Better to start numbering with 0 Always close the file when done make the file executable, execute it in the examples folder

Modify the script so that the header line is not printed virginie@Darwin:~/Documents/BioInfo-cours/pcfb/examples$ ./latlon_1.py 0 : Dive Date Lat Lon Depth Notes 1 : Tiburon 596 19-Jul-03 36 36.12 N 122 22.48 W 1190 holotype 2 : JSL II 1411 16-Sep-86 39 56.4 N 70 14.3 W 518 paratype 3 : JSL II 930 18-Aug-84 40 05.03 N 69 03.01 W 686 Youngbluth (1989) 4 : Ventana 1575 11-Mar-99 36 42.24 N 122 02.52 W 767 5 : Ventana 1777 16-Jun-00 36 42.60 N 122 02.70 W 934 6 : Ventana 2243 9-Sep-02 36 42.48 N 122 03.84 W 1001 7 : Tiburon 515 24-Nov-02 36 42.00 N 122 01.98 W 1156 8 : Tiburon 531 13-Mar-03 24 19.02 N 109 12.18 W 1144 9 : Tiburon 547 31-Mar-03 24 14.04 N 109 40.02 W 1126 10 : JSL II 3457 26-Sep-03 40 17.77 N 68 06.68 W 862 Francesc Pages (pers.comm) virginie@Darwin:~/Documents/BioInfo-cours/pcfb/examples$ #!/usr/bin/env python (...) # Initialize the counter used to keep track of line numbers LineNumber = 0 # Loop through each line in the file for Line in InFile: # Remove the line-ending characters Line=Line.strip('\n') # Print the line print LineNumber,":", Line # Index the counter used to keep track of line numbers LineNumber = LineNumber + 1 # After the loop is completed, close the file InFile.close() Modify the script so that the header line is not printed HINT : Use if … : within the loop

Open latlon_2.py (3) split the line into data fileds Method .split() Produces a list of strings = the values occurring between the delimiters, the delimiters are thrown away .split() considers space and tab as delimiters .split('\t') considers only tab as delimiters #!/usr/bin/env python (...) # Initialize the counter used to keep track of line numbers LineNumber = 0 # Loop through each line in the file for Line in InFile: if LineNumber > 0: # Remove the line ending characters Line=Line.strip('\n') # Separate the line into a list of its tab-delimited components LineList=Line.split('\t') # Print the line # print LineNumber,":", LineList # print LineList[4], LineList[2], LineList[3] print "Depth: %s\tLat: %s\t Lon:%s" % (LineList[4], LineList[2], LineList[3]) # Index the counter used to keep track of line numbers LineNumber = LineNumber + 1 Open latlon_2.py What happens if you use Line.split('\t') instead of Line.split('\t') ?

Open latlon_3.py (4) write the new output file 1 Function open #!/usr/bin/env python (...) LineNumber = 0 # Open the output file for writing -Do this *before* the loop, not inside it OutFileName=InFileName + ".kml" OutFile=open(OutFileName,'w') # You can append instead with 'a' # Loop through each line in the file for Line in InFile: # Skip the header, line # 0 if LineNumber > 0: # Remove the line ending characters Line=Line.strip('\n') ElementList=Line.split('\t') # Use the % operator to generate a string # We can use this for output both to the screen and to a file OutputString = "Depth: %s\tLat: %s\t Lon:%s" % \ (ElementList[4], ElementList[2], ElementList[3]) # Can still print to the screen then write to a file print OutputString # Unlike print statements, .write needs a linefeed OutFile.write(OutputString+"\n") # Index the counter used to keep track of line numbers LineNumber = LineNumber + 1 # After the loop is completed, close the files InFile.close() OutFile.close() Open latlon_3.py 1 Function open (file name, file mode) 'w' = write mode 'a' = append mode 2 Be careful of not deleting existing files >> versus > 3 4