Download Reading files in Python - School of Information Technologies

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Structured programming wikipedia , lookup

Diff wikipedia , lookup

Transcript
Reading files in Python
Girls’ Programming Network
School of Information Technologies
University of Sydney
Mini-lecture 10
open
reading
Examples
Summary
TODO
2
Outline
1
opening files
2
Reading from a file
3
Examples
4
Summary
5
TODO
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
3
To read a file you need to open it first
ˆ the open function creates an open file
ˆ we need to pass in the name of the file ('test.txt')
1
2
3
4
>>> f = open('test.txt', 'rU')
>>> f
<open file 'test.txt', mode 'rU' at 0x63380>
>>>
ˆ you can read any file but text files are easiest
ˆ text files can be created in Notepad or IDLE (or Word)
ˆ the 'rU' is for universal newline mode
(for now just always remember to put it in!)
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
4
A missing file causes an error
ˆ here we try to open a file that doesn’t exist:
1
2
3
4
5
>>> f = open('missing.txt', 'rU')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'missing.txt'
>>>
ˆ the result is an input/output error (an IOError exception)
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
5
Where is the file?
ˆ The file is assumed to be in the current directory
ˆ If not, you need to explicitly give the absolute path
ˆ You specify an absolute path starting with a drive (e.g. C:)
1
>>> f = open('C:/test.txt', 'rU')
ˆ opens up C:\test.txt regardless of where the program runs
Girls’ Programming Network
Reading files
Mini-lecture 10
open
An example file called
1
2
3
reading
Examples
Summary
TODO
6
test.txt
This is the first line in the file.
This is the second line in the file.
This is the third line in the file.
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
7
You can read a whole file into a string
1
2
3
4
5
6
7
8
>>> f = open('test.txt', 'rU')
>>> s = f.read()
>>> s
'This is the first line in the file.\nThis is the second lin
>>> print s
This is the first line in the file.
This is the second line in the file.
This is the third line in the file.
9
10
>>>
ˆ the read method reads in the whole file
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
8
You can also read in one line at a time
ˆ and all it takes is a for loop
1
2
3
4
5
>>> f = open('test.txt', 'rU')
>>> for line in f:
...
print line
...
This is the first line in the file.
6
7
This is the second line in the file.
8
9
This is the third line in the file.
10
11
>>>
ˆ Why is there an extra blank line each time?
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
9
Chomping off the newline
ˆ The newline character is represented by '\n':
1
2
3
4
>>> print 'Hello\nWorld'
Hello
World
>>>
ˆ It is kept on each line during reading
ˆ We can remove it lots of ways:
1
2
3
4
5
6
>>> x = 'abc\n'
>>> x.strip()
'abc'
>>> x[:-1]
'abc'
>>>
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
10
Reading each line of a file
1
2
3
4
5
6
7
>>> for line in open('test.txt', 'rU'):
...
line = line.strip()
...
print line
This is the first line in the file.
This is the second line in the file.
This is the third line in the file.
>>>
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
11
Reading a file by columns
1
2
3
4
5
6
7
>>> for line in open('test.txt', 'rU'):
...
cols = line.split()
...
print cols[3]
first
second
third
>>>
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
12
Reading a file by words
1
2
3
4
5
6
7
8
9
10
>>> for line in open('test.txt', 'rU'):
...
for word in line.split():
...
print word
This
is
the
first
line
...
>>>
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
13
You should now be able to:
ˆ Open files in Python
ˆ Read the whole file into a string
ˆ Read each line of a file one at a time
Girls’ Programming Network
Reading files
Mini-lecture 10
open
reading
Examples
Summary
TODO
14
Try these:
ˆ create a short text file in Notepad
ˆ read it in line and word at a time in Python
ˆ Download Pride and Prejudice from Project Gutenberg
ˆ print it out in Python
ˆ count the number of lines
ˆ count the number of lines containing Elizabeth
ˆ print it out word at a time
ˆ count the number of times the word Darcy appears
ˆ find the most frequent word (this one’s a bit hard!)
Girls’ Programming Network
Reading files
Mini-lecture 10