Skip to content

Instantly share code, notes, and snippets.

@lindsaymarkward
Created February 21, 2020 04:40
Show Gist options
  • Save lindsaymarkward/6d33541f49625579eacd8c6471b11deb to your computer and use it in GitHub Desktop.
Save lindsaymarkward/6d33541f49625579eacd8c6471b11deb to your computer and use it in GitHub Desktop.
Split one text string Table Of Contents (copied from PDF) into readable paragraph/list form
text = "Chapter 1 Introduction to Computers and Programming 19 1.1 Introduction 19 1.2 Hardware and Software 20 1.3 How Computers Store Data 25 1.4 How a Program Works 30 1.5 Using Python 38 Chapter 2 Input, Processing, and Output 49 2.1 Designing a Program 49 2.2 Input, Processing, and Output 53 2.3 Displaying Output with the print Function 54 2.4 Comments 57 2.5 Variables 58 2.6 Reading Input from the Keyboard 67 2.7 Performing Calculations 71 2.8 More About Data Output 83 Chapter 3 Decision Structures and Boolean Logic 99 3.1 The if Statement 99 3.2 The if-else Statement 108 3.3 Comparing Strings 111 3.4 Nested Decision Structures and the if-elif-else Statement 115 3.5 Logical Operators 123 3.6 Boolean Variables 129 Chapter 4 Repetition Structures 139 4.1 Introduction to Repetition Structures 139 4.2 The while Loop: A Condition-Controlled Loop 140 4.3 The for Loop: A Count-Controlled Loop 148 4.4 Calculating a Running Total 159 4.5 Sentinels 162 4.6 Input Validation Loops 165 4.7 Nested Loops 170 Chapter 5 Functions 183 5.1 Introduction to Functions 183 5.2 Defining and Calling a Void Function 186 5.3 Designing a Program to Use Functions 191 5.4 Local Variables 197 5.5 Passing Arguments to Functions 199 5.6 Global Variables and Global Constants 209 5.7 Introduction to Value-Returning Functions: Generating Random Numbers 213 5.8 Writing Your Own Value-Returning Functions 224 5.9 The math Module 235 5.10 Storing Functions in Modules 238 Chapter 6 Files and Exceptions 253 6.1 Introduction to File Input and Output 253 6.2 Using Loops to Process Files 270 6.3 Processing Records 277 6.4 Exceptions 290 Chapter 7 Lists and Tuples 309 7.1 Sequences 309 7.2 Introduction to Lists 309 7.3 List Slicing 317 7.4 Finding Items in Lists with the in Operator 320 7.5 List Methods and Useful Built-in Functions 321 7.6 Copying Lists 328 7.7 Processing Lists 330 7.8 Two-Dimensional Lists 342 7.9 Tuples 346 Chapter 8 More About Strings 357 8.1 Basic String Operations 357 8.2 String Slicing 365 8.3 Testing, Searching, and Manipulating Strings 369 Chapter 9 Dictionaries and Sets 387 9.1 Dictionaries 387 9.2 Sets 410 9.3 Serializing Objects 422 Chapter 10 Classes and Object-Oriented Programming 437 10.1 Procedural and Object-Oriented Programming 437 10.2 Classes 441 10.3 Working with Instances 458 10.4 Techniques for Designing Classes 480 Chapter 11 Inheritance 499 11.1 Introduction to Inheritance 499 11.2 Polymorphism 514"
text = text.replace("Chapter ", "")
words = text.split()
for i, word in enumerate(words):
try:
int(word)
words[i] = "\n"
except ValueError:
continue
paragraph = " ".join(words)
print(paragraph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment