Seek Method and CSV Files

Slides:



Advertisements
Similar presentations
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Advertisements

MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW Indexing Large Data COMP
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
Chapter 14 Files and Exceptions II. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What we already.
Miscellaneous Excel Combining Excel and Access. – Importing, exporting and linking Parsing and manipulating data. 1.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
Chapter 17 Creating a Database.
FILE HANDLING IN C++.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
EXCEL Intro to Microsoft Excel. Objectives for the Week Content ObjectivesLanguage Objectives I can create and manipulate charts, graphs, and reports.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Files Tutor: You will need ….
Chapter 13.3: Databases Invitation to Computer Science, Java Version, Second Edition.
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Files Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Chapter 14: Sequential Access Files
Spreadsheets.
Topic: File Input/Output (I/O)
Miscellaneous Excel Combining Excel and Access.
CSC201: Computer Programming
Creates the file on disk and opens it for writing
Chapter 7 Text Input/Output Objectives
Net 323 D: Networks Protocols
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
CS190/295 Programming in Python for Life Sciences: Lecture 1
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Lecture 13 Input/Output Files.
Seek Method and CSV Files
Java for Teachers Intermediate
C Programming Lecture-15 File I/O
EECE.4810/EECE.5730 Operating Systems
File IO and Strings CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
files Dr. Bhargavi Goswami Department of Computer Science
Creates the file on disk and opens it for writing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Net 323 D: Networks Protocols
File Handling.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Files Handling In today’s lesson we will look at:
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Chapter 15 Files, Streams and Object Serialization
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions By Anand George.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
How to read from a file read, readline, reader
Introduction to Computer Science
CS313T Advanced Programming language
Presentation transcript:

Seek Method and CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg

What is a File? A file is a collection of data that is stored on secondary storage like a disk or a thumb drive. Accessing a file means establishing a connection between the file and the program and moving data between the two.

Two Types of Files Files come in two general types: Text files: files where control characters such as “\n” are translated. These are generally human readable Binary files: all the information is taken directly without translation. Not readable and contains non-readable info.

Making and using a File Object fileObject = open(“myFile.txt”, “r”) Opens the file “myFile.txt” in read-only mode fileObject.read() Reads the entire contents of the file as a string and returns it. fileObject.readline() Delivers the next line as a string.

Making and using a File Object fileObject.readLines() Returns a single list of all the lines from the file. for line in fileObject: Iterator to go through the lines of a file. fileObject.write(s) writes the string s to the file fileObject.writelines(list) write a list of strings (one at a time) to the file

Making and using a File Object fileObject.close() When done, you close the file.

Moving Around a File Every time you issue a read or write operation on a file, you move around a file pointer. Keeps track of where to start reading/writing next You can change the file pointer Use the seek method

Seek fileObject.seek(offset, fromWhat) Go to a particular byte offset in the file fromWhat: 0 means from the beginning of the file 1 uses the current file position 2 uses the end of the file as the reference point

Seek Example fin = open(“test.txt”,”r”) #Seek to 10 bytes from the beginning of the file fin.seek(10,0) #Read the byte at that position byte = fin.read(1) print(“We read”, byte)

Spreadsheets The spreadsheet is a very popular, and powerful, application for manipulating data. Its popularity means there are many companies that provide their own version of the spreadsheet. It would be nice if those different versions could share their data…

CSV, Basic Sharing A basic approach to share data is the comma separated value (CSV) format: it is a text format, accessible to all apps each line (even if blank) is a row in each row, each value is separated from the others by a comma (even if it is blank) cannot capture complex things like formula

Athlete and BMI Example Demo (see athletes_data.csv and bmi2.py files)

Using the split() function for athlete in fin: last,first,height,weight,country,gender,sport=athlete.split(",") Split will split up the line by the argument (in this example, it is a comma Split will save the first string value into the first variable on the left side of the comma E.g. first string gets saved in variable “last”, second gets saved in variable “height” Each line must have the exact same number of variables