Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python printing to file

I'm stuck on this homework assignment that I have.
Below are the instructions for it
Quote:You are an English major who has been given assignment to analyze Dr. King's
"I Have A Dream" speech.
Open the file that contains Dr. King’s “I have a dream” speech.
Print the total number of characters in the speech.
Open another file for output.
Write only the first paragraph of this speech to that file.


I finished the first 2 steps in the assignment which is in the code below:
Code
# Open the file
inputFile = open("mlkspeech.txt", "r")

# Read the file
data = inputFile.read()

# Determine # of characters in file
numberOfCharacters = len(data)

print("Number of Characters in the file:", numberOfCharacters)


inputFile.close()

How would I open and create a new file and put the first paragraph of the MLK speech into it?
I believe we are supposed to use a while loop to do that

Thanks!
Code
# Create the file with the 'w' option and alias the handle
with open("dream-first-para.txt", "w") as outputFile:
    # Find the index of the newline escape sequence
    endOfParagraph = data.find("\n\n")

    # Substring the from start to the newline index
    firstParagraph = data[:endOfParagraph]

    # Write the first paragraph
    outputFile.write(firstParagraph)
   
# With will terminate the file handle for you

Add this to the bottom of your current code and it should work.
(Sep 21, 2021 02:52 PM)Donkey Death Jam Wrote:  
Code
# Create the file with the 'w' option and alias the handle
with open("dream-first-para.txt", "w") as outputFile:
    # Find the index of the newline escape sequence
    endOfParagraph = data.find("\n\n")

    # Substring the from start to the newline index
    firstParagraph = data[:endOfParagraph]

    # Write the first paragraph
    outputFile.write(firstParagraph)
   
# With will terminate the file handle for you

Add this to the bottom of your current code and it should work.

Would there be another way to do it without using
Quote: # Substring the from start to the newline index
firstParagraph = data[:endOfParagraph]


Also, I appreciate the help :)
(Sep 21, 2021 03:04 PM)Will Patterson Wrote:  Would there be another way to do it without using
Also, I appreciate the help :)

There are many ways to do it. If you want an explicit loop, you can do:

Code
# Create the file with the 'w' option
with open("dream-first-para.txt", "w") as outputFile:
    # Loop through every character in the string
    newLineCount = 0
    for character in data:
        # Detect two new lines in a row
        if character == "\n":
            newLineCount += 1
        else:
            newLineCount = 0
            
        # If two in a row, we have written the paragraph, abort loop
        if (newLineCount > 1):
            break
            
        outputFile.write(character)
    
# With will terminate the file handle for you
use open() in python. You can use open with close, or use with open(). You can do searches on it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Java] by severosJun 5, 2021 06:30 PM 4 350 Jun 5, 2021 06:30 PM
Last Post: GenericSkid
[Python] by HardStoNJul 14, 2020 08:10 AM 5 321 Jul 14, 2020 08:10 AM
Last Post: Anxuein