Lecture 29 Python III: Text-Editing in Python

Download Report

Transcript Lecture 29 Python III: Text-Editing in Python

Lecture # 29 Python III:
Editing a Text File in Python
Create a Text File:
Georgie Porgie pudding and .py,
Kissed the girls and made them cry
When the boys came out to play,
Georgie Porgie ran away.
Create a “Search & Replace”
Python Program that will:
• Open the text file
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
• Search the text file to find the name “Georgie”
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
• Search the text file to find the name “Georgie”
• Swap that name in place of “Georgie”
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
• Search the text file to find the name “Georgie”
• Swap that name in place of “Georgie”
• Create and open a new file
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
• Search the text file to find the name “Georgie”
• Swap that name in place of “Georgie”
• Create and open a new file
• Output the edited string to the new file
Create a “Search & Replace”
Python Program that will:
• Open the text file
• Input replacement text (name) from the keyboard
• Search the text file to find the name “Georgie”
• Swap that name in place of “Georgie”
• Create and open a new file
• Output the edited string to the new file
• Close the new file
Create a “Search & Replace”
Python Program that will:
• Open the text file:
f = open(pickAFile())
Create a “Search & Replace”
Python Program that will:
• Input replacement text (name) from the keyboard:
Create a “Search & Replace”
Python Program that will:
• Input replacement text (name) from the keyboard:
r = raw_input("Input replacement name: ”)
Create a “Search & Replace”
Python Program that will:
• Search the text file to find the name “Georgie”:
s = f.read()
reads text file containing “Georgie”
into string, s
s.find(“Georgie”)
finds “Georgie” in string, s
returns index of the “G”.
Georgie Porgie pudding and
index
0123456789…
Create a “Search & Replace”
Python Program that will:
In this case index = 0, for first occurrence of “Georgie.”
Georgie Porgie pudding and
index 0123456789…
In general, s.find returns index of first occurrence of
substring that is found.
Create a “Search & Replace”
Python Program that will:
• Swap that name in place of “Georgie”:
let
r = “Jimmy”
p = “Georgie”
Create a “Search & Replace”
Python Program that will:
• Swap that name in place of “Georgie”:
let
r = “Jimmy”
p = “Georgie”
find Georgie between index 0
and len(s)
skip past
Georgie
t = r + s[s.find("Georgie",0,len(s))+len(p):len(s)]
Create a “Search & Replace”
Python Program that will:
• Swap that name in place of “Georgie”:
let
r = “Jimmy”
p = “Georgie”
find Georgie between index 0
and len(s)
skip past
Georgie
t = r + s[s.find("Georgie",0,len(s))+len(p):len(s)]
0
+
7
t = Jimmy + _Porgie pudding and .py, …
Georgie Porgie pudding and .py,
01234567
Create a “Search & Replace”
Python Program that will:
• Since there are actually 2 occurrences of “Georgie”:
Do
find Georgie between index 0
and len(s)
t = r + s[s.find("Georgie",0,len(s))+len(p):
s.find("Georgie",len(p),len(s))]
s[n:m] references the portion of string s that we want to
concatenate to r.
Create a “Search & Replace”
Python Program that will:
• Since there are actually 2 occurrences of “Georgie”:
Do
find Georgie between index 0
and len(s)
t = r + s[s.find("Georgie",0,len(s))+len(p):
s.find("Georgie",len(p),len(s))] +
r + s[s.find("Georgie",len(p),len(s)) +
len(p):len(s)]
Create a “Search & Replace”
Python Program that will:
• Create and open a new (edied) file:
Borrow from f
g = open(“… put in file path here …”
+r
(Call file new name, Jimmy)
+ “.txt”,”wt”)
- text extension
- open for writing
Create a “Search & Replace”
Python Program that will:
• Output the edited string, t, to the new file:
g.write(t)
g.close()
And be sure to close the file
Algorithms for Basic Word
Processing
Type a character
• Delete a character
• Select some characters
• Bold some characters
• Cut / Copy / Paste
Cursor Position:
an index into the string
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
Cursor = 3
Cursor=6
6
7
Typing a Character
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
A
2
3
4
5
6
7
s
p
t
r
i
n
Cursor = 3
Key = ‘p’
g
65
32 115 112 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor=4
Typing a Character
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Cursor = 3
Key = ‘p’
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] }
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
t
r
i
n
g
g
65
32 115 116 114 105 110 103 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=8
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] } move character up one space
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
t
r
i
n
n
g
65
32 115 116 114 105 110 110 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=7
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] } move character up one space
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
t
r
i
i
n
g
65
32 115 116 114 105 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=6
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] } move character up one space
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
t
r
r
i
n
g
65
32 115 116 114 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=5
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] } move character up one space
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
t
t
r
i
n
g
65
32 115 116 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=4
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] } move character up one space
Text[Cursor] = Key;
Cursor = Cursor+1;
Typing a Character
A
s
p
t
r
i
n
g
65
32 115 112 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = ‘p’
I=3
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] }
Text[Cursor] = Key; add the typed character
Cursor = Cursor+1;
Typing a Character
A
s
p
t
r
i
n
g
65
32 115 112 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = ‘p’
I=3
For ( index I starting at Text.length-1 down to Cursor)
{
Text[I+1]=Text[I] }
Text[Cursor] = Key;
Cursor = Cursor+1; move the cursor over
Algorithms for Basic Word
Processing
• Type a character
Delete a character
• Select some characters
• Bold some characters
• Cut / Copy / Paste
Delete a Character
A
s
p
t
r
i
n
g
65
32 115 112 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=?
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
t
r
i
n
g
65
32 115 116 116 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=3
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
r
i
n
g
65
32 115 116 114 114 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=4
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
i
i
n
g
65
32 115 116 114 105 105 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=5
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
i
n
n
g
65
32 115 116 114 105 110 110 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=6
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
i
n
g
g
65
32 115 116 114 105 110 103 103
0
1
2
3
4
5
6
7
8
Cursor = 4
Key = delete
I=7
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] } move a character down one space
Cursor = Cursor-1;
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
i
n
g
g
65
32 115 116 114 105 110 103 103
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = delete
I=8
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] }
Cursor = Cursor-1; move the cursor over
Text[Text.length-1] = “no character”
Delete a Character
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103 ###
0
1
2
3
4
5
6
7
8
Cursor = 3
Key = delete
I=8
For ( index I starting at Cursor+1 up to Text.length-1)
{
Text[I-1]=Text[I] }
Cursor = Cursor-1;
Text[Text.length-1] = “no character”; blank out last character
A Helpful Function
Function moveChars(Text,Start,End,Distance)
{ if (Distance>0)
{
for (index I from End down
{ Text[I+Distance]=Text[I];}
to Start)
}
Else
{
}
}
for (index I from Start up to
{ Text[I+Distance]=Text[I] }
End)
Typing a Character
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Cursor = 3
Key = ‘p’
moveChars(text,Cursor,text.length-1, 1);
Text[Cursor]=Key;
Cursor=Cursor+1;
Delete a Character
A
s
p
t
r
i
n
g
65
32 115 112 116 114 105 110 103
0
1
2
3
4
5
6
7
8
moveChars(text,cursor,text.length-1,-1);
Cursor=Cursor-1;
text[text.length -1]=no character;
Cursor = 4
Key = delete
I=?
Algorithms for Basic Word
Processing
• Type a character
• Delete a character
Select some characters
• Bold some characters
• Cut / Copy / Paste
Selecting Characters
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 7
Algorithms for Basic Word
Processing
• Type a character
• Delete a character
• Select some characters
Bold some characters
• Cut / Copy / Paste
Bolding Characters
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103 ### ###
0
1
2
3
4
5
6
7
8
moveChars(text,End,text.length-1,2);
moveChars(text,Start,End-1,1);
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
9
Start = 2
End = 7
Bolding Characters
A
s
t
r
i
n
g
g
65
32 115 116 114 105 110 103 ### 103
0
1
2
3
4
5
6
7
8
9
Start = 2
End = 7
moveChars(text,End,text.length-1,2); -> moveChars(text,7,7,2)
moveChars(text,Start,End-1,1);
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
Bolding Characters
A
s
s
t
r
i
n
g
65
32 115 115 116 114 105 110 ### 103
0
1
2
3
4
5
6
7
8
9
Start = 2
End = 7
moveChars(text,End,text.length-1,2);
moveChars(text,Start,End-1,1); -> moveChars(text,2,6,1)
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
Bolding Characters
A
## s
t
r
i
n
g
65
32 <b> 115 116 114 105 110 ### 103
0
1
2
3
4
5
6
7
8
moveChars(text,End,text.length-1,2);
moveChars(text,Start,End-1,1);
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
9
Start = 2
End = 7
Bolding Characters
A
## s
t
r
i
n
## g
65
32 <b> 115 116 114 105 110 </b> 103
0
1
2
3
4
5
6
7
8
moveChars(text,End,text.length-1,2);
moveChars(text,Start,End-1,1);
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
9
Start = 2
End = 7
Bolding Characters
A
## s
t
r
i
n
## g
65
32 <b> 115 116 114 105 110 </b> 103
0
1
2
3
4
5
6
7
8
moveChars(text,End,text.length-1,2);
moveChars(text,Start,End-1,1);
Text[Start]=code for start bold;
Text[End+1]=code for end bold;
End=End+2;
9
Start = 2
End = 9
Algorithms for Basic Word
Processing
• Type a character
• Delete a character
• Select some characters
• Bold some characters
Cut / Copy / Paste
Cut
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 7
ClipBoard=
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
moveChars(text,End,text.length-1,Start-End);
Set the last (End-Start) characters in the array to “no character”
End=Start;
Cut
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 7
ClipBoard=“strin”
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
moveChars(text,End,text.length-1,Start-End);
Set the last (End-Start) characters in the array to “no character”
End=Start;
Cut
A
g
t
r
i
n
g
65
32 103 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 7
ClipBoard =“strin”
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
moveChars(text,End,text.length-1,Start-End);
Set the last (End-Start) characters in the array to “no character”
End=Start;
Cut
A
g
65
32 103 ### ### ### ### ###
0
1
2
3
4
5
6
7
Start = 2
End = 7
ClipBoard =“strin”
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
moveChars(text,End,text.length-1,Start-End);
Set the last (End-Start) characters in the array to “no character”
End=Start;
Cut
A
g
65
32 103 ### ### ### ### ###
0
1
2
3
4
5
6
7
Start = 2
End = 2
ClipBoard =“strin”
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
moveChars(text,End,text.length-1,Start-End);
Set the last (End-Start) characters in the array to “no character”
End=Start;
Copy
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 7
ClipBoard =“strin”
For (each character C with index >= Start and < End)
{
copy C to the ClipBoard }
Paste
A
s
65
32 115
0
1
2
Start = 2
End = 2
ClipBoard =“strin”
moveChars(text,End,text.length-1, ClipBoard.length);
Copy the characters from ClipBoard into the array at index Start
Paste
A
g
g
65
32 103 ### ### ### ### 103
0
1
2
3
4
5
6
7
Start = 2
End = 2
ClipBoard =“strin”
moveChars(text,End,text.length-1, ClipBoard.length);
Copy the characters from ClipBoard into the array at index Start
Paste
A
s
t
r
i
n
g
65
32 115 116 114 105 110 103
0
1
2
3
4
5
6
7
Start = 2
End = 2
ClipBoard =“strin”
moveChars(text,End,text.length-1, ClipBoard.length);
Copy the characters from ClipBoard into the array at index Start
More about Python
String Encodings
• ASCII
– special characters:
\n = Linefeed
\r = Carriage return
\t = Horizontal Tab
• Unicode
– u”…..”
Accessing Substrings
• str[i]
• len(str)
• slices
–
–
–
–
str[n:m]
str[:m]
str[n:]
str[:]
– getting the ith character in str
– returns the length of the str
– str[n] through str[m-1]
– str[0] through str[m-1]
– str[n] through str[len(str)-1]
– str[0] through str[len(str)-1]
• The entire string
– str[-1:]
– str[len(str)-1] through str[len(str)-1]
• A sequence with 1 character, the last character
– str[:-1]
• Demo
– str[0] through str[len(str)-2]
String Methods
• String methods
– capitalize() – only the first
word in the string
– startswith(prefix) – returns
1 or 0
– endswith(suffix)
– find(str), find(str, start),
find(str, start, end)
• returns -1 if string not found
– rfind variant
– upper(), lower(),
swapcase()
• String methods
– isalpha()
– isdigit()
– replace(string,
replacement)
Importing Modules
• Decomposing a program into parts or modules
• Module is a python file
– Ends in “.py”
• Python file contains methods, variables, and classes
• Forms of import
– import file_name
• Assumes file_name ends in “.py”
• Must use full path name to access member of module
– from file_name import *
– from file_name import x, y, z
– import x as y
Examples
• Form Letter – Program_91.py (and command line)
• changeLittle Example – Program_92.py (and command line)
– use sample.py as first parameter
• findSequence – Program_93.py (and command line)
• replaceAllOccurrences – Program_94.py
– use sample.py again
• Web Scraping – Program_95.py
• titleDirectory – Program_96.py
– setMediaPath to MediaSources
• random – Program_97.py
• random sentences
– Execution of a module from command line
• Program_97a.py
• Program_97b.py
– Including a main routine
• import97a.py
• import97b.py
Built in Python Modules
(The Python library)
• Often used modules
–
–
–
–
–
–
–
os, sys, random
datetime, calendar
calendar
math
zipfile
email
SimpleHTTPServer
• Why use modules
– Save work, why reinvent the wheel?
– Higher quality
• Fewer bugs
• Run faster
• Well documented
Lists
• Literals
– a = [1, 2, 3, 4]
• Access
– a[i]
• for loops
– for i in a:
• Concatenation
– “+” operator
• Lists of lists
– access – a[i][j]
• Methods
–
–
–
–
–
–
append(element)
remove(something)
sort()
reverse()
count()
split(delimeter)
• strings to lists of strings
• Functions
– max(list)
– min(list)
Files
• List of bytes
• Name
• Suffix
– Type of information in file
• Folder or Directory Structure
– Trees
• Trees as lists of lists of lists of …
– Traversing a tree with dot notation
• From root
– Traversing domain names
• students.cs.byu.edu