Description
There is a file joke.txt, which reads as follows:
Teacher: The choppy radicals are three points of water. Ming Li A similar example:
Xiao Ming: Pie wonton?
Teacher: Let's change one.
Xiao Ming: Play glass ball.
Teacher: Not good.
Xiao ming:... Can't bathe?
Teacher: Have the ability to have a six word?
Xiao Ming: Oh, MI-mi-coax.
Teacher: a 100-word!
Xiao Ming: haha haha haha ...
Teacher: Get outta here!!!
Output it in the following way:
The teacher said : The choppy radicals are three points of water. Ming Li A similar example:
Xiao Ming said: Pie wonton?
The teacher said, "Let's change one."
Xiao Ming said: Play glass ball.
The teacher said: "Not good."
Xiao Ming said: ... Can't bathe?
The teacher said: "Have the ability to a six words?"
Xiao Ming said, "Oh, MI-mi-Coax."
The teacher said: "To a 100 words!"
Xiao Ming said: haha haha haha ...
Teacher said: Get out!!!
The code is as follows:
Import OS # importing System Libraries
Try
data = open (' Joke.txt ') # opening file
Data.seek (0) # Navigate to file start location
For each_line in data:
Try:
(role, word) = Each_line.split (': ', 1) # Parse the data, the parameter ' 1 ' in Split means only the first ': ' Is parsed
print (role, end= ') # prints in the new format, which can also be added to the list
print (' Say: ', end= ')
print (Word, end= ")
except ValueError: # exception handling
print ('------valueerror occur! ')
Except IOError:
Print ('---The file is missing. ')
Finally
Data.close () # Close File
Python file Content parsing (1)