Download presentation
Presentation is loading. Please wait.
Published byMark Owen Modified over 8 years ago
1
Lecture # 29 Python III: Editing a Text File in Python
2
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.
3
Create a “Search & Replace” Python Program that will: Open the text file
4
Create a “Search & Replace” Python Program that will: Open the text file Input replacement text (name) from the keyboard
5
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”
6
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”
7
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
8
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
9
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
10
Create a “Search & Replace” Python Program that will: Open the text file: f = open(pickAFile())
11
Create a “Search & Replace” Python Program that will: Input replacement text (name) from the keyboard:
12
Create a “Search & Replace” Python Program that will: Input replacement text (name) from the keyboard: r = raw_input("Input replacement name: ”)
13
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…
14
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.
15
Create a “Search & Replace” Python Program that will: Swap that name in place of “Georgie”: let r = “Jimmy” p = “Georgie”
16
Create a “Search & Replace” Python Program that will: Swap that name in place of “Georgie”: let r = “Jimmy” p = “Georgie” t = r + s[s.find("Georgie",0,len(s))+len(p):len(s)] find Georgie between index 0 and len(s) skip past Georgie
17
Create a “Search & Replace” Python Program that will: Swap that name in place of “Georgie”: let r = “Jimmy” p = “Georgie” t = r + s[s.find("Georgie",0,len(s))+len(p):len(s)] t = Jimmy + _Porgie pudding and.py, … Georgie Porgie pudding and.py, 01234567 0 + 7 find Georgie between index 0 and len(s) skip past Georgie
18
Create a “Search & Replace” Python Program that will: Since there are actually 2 occurrences of “Georgie”: Do 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. find Georgie between index 0 and len(s)
19
Create a “Search & Replace” Python Program that will: Since there are actually 2 occurrences of “Georgie”: Do 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)] get the second occurrence of Georgie find Georgie between index 0 and len(s)
20
Create a “Search & Replace” Python Program that will: Create and open a new (edied) file: g = open(“… put in file path here …” + r (Call file new name, Jimmy) + “.txt”,”wt”) - text extension - open for writing Borrow from f
21
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
22
Algorithms for Basic Word Processing Type a character Delete a character Select some characters Bold some characters Cut / Copy / Paste
23
Cursor Position: an index into the string Cursor = 3 Cursor=6
24
Typing a Character Cursor=4 Cursor = 3 Key = ‘ p ’
25
Typing a Character 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;
26
Typing a Character 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;
27
Typing a Character 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;
28
Typing a Character 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;
29
Typing a Character 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;
30
Typing a Character 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;
31
Typing a Character 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;
32
Typing a Character 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
33
Algorithms for Basic Word Processing Type a character Delete a character Select some characters Bold some characters Cut / Copy / Paste
34
Delete a Character 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 ”
35
Delete a Character 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 ”
36
Delete a Character 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 ”
37
Delete a Character 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 ”
38
Delete a Character 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 ”
39
Delete a Character 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 ”
40
Delete a Character 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 ”
41
Delete a Character 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
42
A Helpful Function Function moveChars(Text,Start,End,Distance) {if (Distance>0) {for (index I from End down to Start ) { Text[I+Distance]=Text[I];} } Else {for (index I from Start up to End ) { Text[I+Distance]=Text[I] } }
43
Typing a Character Cursor = 3 Key = ‘ p ’ moveChars(text,Cursor,text.length-1, 1); Text[Cursor]=Key; Cursor=Cursor+1;
44
Delete a Character Cursor = 4 Key = delete I=? moveChars(text,cursor,text.length-1,-1); Cursor=Cursor-1; text[text.length -1]=no character;
45
Algorithms for Basic Word Processing Type a character Delete a character Select some characters Bold some characters Cut / Copy / Paste
46
Selecting Characters Start = 2 End = 7
47
Algorithms for Basic Word Processing Type a character Delete a character Select some characters Bold some characters Cut / Copy / Paste
48
Bolding Characters Start = 2 End = 7 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;
49
Bolding Characters 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;
50
Bolding Characters 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;
51
Bolding Characters Start = 2 End = 7 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;
52
Bolding Characters Start = 2 End = 7 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;
53
Bolding Characters Start = 2 End = 9 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;
54
Algorithms for Basic Word Processing Type a character Delete a character Select some characters Bold some characters Cut / Copy / Paste
55
Cut 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;
56
Cut 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;
57
Cut 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;
58
Cut 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;
59
Cut 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;
60
Copy Start = 2 End = 7 ClipBoard = “ strin ” For (each character C with index >= Start and < End) { copy C to the ClipBoard }
61
Paste 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
62
Paste 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
63
Paste 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
64
More about Python
65
String Encodings ASCII –special characters: \n = Linefeed \r = Carriage return \t = Horizontal Tab Unicode –u”…..”
66
Accessing Substrings str[i] – getting the i th character in str len(str) – returns the length of the str slices –str[n:m] – str[n] through str[m-1] –str[:m] – str[0] through str[m-1] –str[n:] – str[n] through str[len(str)-1] –str[:] – 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] – str[0] through str[len(str)-2] Demo
67
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)
68
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
69
Examples Form Letter – Program_91.py (and command line)Program_91.py changeLittle Example – Program_92.py (and command line)Program_92.py –use sample.py as first parameter findSequence – Program_93.py (and command line)Program_93.py replaceAllOccurrences – Program_94.pyProgram_94.py –use sample.py again Web Scraping – Program_95.pyProgram_95.py titleDirectory – Program_96.pyProgram_96.py –setMediaPath to MediaSources random – Program_97.pyProgram_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
70
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
71
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)
72
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.