lbobgdt-07-python text file processing
lbobgdt-07-python text file processing
BIGDATA
Big Data Techniques
and Technologies
Bobby Reyes
1
1/23/2025
Strings
◼ string: A sequence of text characters in a program.
◼ Strings start and end with quotation mark " or apostrophe ' characters.
◼ Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"
◼ A string may not span across multiple lines or contain a " character.
"This is not
a legal String."
"This is not a "legal" String either."
Indexes
◼ Characters in a string are numbered with indexes starting at 0:
◼ Example:
name = "P. Diddy"
index 0 1 2 3 4 5 6 7
character P . D i d d y
◼ Example:
print(name, "starts with", name[0])
Output:
P. Diddy starts with P
2
1/23/2025
String Properties
◼ len(string) - number of characters in a string
(including spaces)
◼ str.lower(string) - lowercase version of a string
◼ str.upper(string) - uppercase version of a string
◼ Example:
name = "Martin Douglas Stepp"
length = len(name)
big_name = str.upper(name)
print(big_name, "has", length, "characters")
Output:
MARTIN DOUGLAS STEPP has 20 characters
input
◼ input : Reads a string of text from user input.
◼ Example:
name = input("Howdy, pardner. What's yer name? ")
print(name, "... what a silly name!")
Output:
Howdy, pardner. What's yer name? Sixto Dimaculangan
Sixto Dimaculangan ... what a silly name!
3
1/23/2025
Text Processing
◼ text processing: Examining, editing, formatting text.
◼ often uses loops that examine the characters of a string one by one
4
1/23/2025
5
1/23/2025
fh.close()
Output:
The first line of text
The second line of text
The third line of text
…
6
1/23/2025
readline() functions
◼ Other in-built file reading commands:
2. <fileobject>.readline() - returns one line at a time
fh = open('Practice_File.txt', 'r’)
Output:
print(fh.readline())
The first line of text
7
1/23/2025
8
1/23/2025
Termination of Input
◼ Two ways to stop reading input:
1. By reading a definite number of items.
2. By the end of the file.
fp.close()
print(pointlist)