Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:44:11+00:00 2026-06-15T23:44:11+00:00

There are two sentnecs in test.txt sentence1 = A sentence is a grammatical unit

  • 0

There are two sentnecs in “test.txt”

sentence1 = A sentence is a grammatical unit consisting of one or more words.

sentence2 = A sentence can also be defined in orthographic terms alone.

count_line = 0
for line in open('C:/Users/Desktop/test.txt'):
    count_line = count_line +1
    fields = line.rstrip('\n').split('\t')
    ##print count_line, fields
    file = open('C:/Users/Desktop/test_words.txt', 'w+')
    count_word = 0
    for words in fields:
        wordsplit = words.split()
        for word in wordsplit:
             count_word = count_word + 1
             print count_word, word
             file.write(str(count_word) + " " + word + '\n')
        file.close()

My result in “test_words.txt” showed only the words from second sentence:

1 A 
2 sentence
3 can
4 also
5 be
6 defined
7 in
8 orthographic
9 terms
10 alone.

How to also write the words from the first sentence in and follow by the words in second sentence “test_words.txt” ?

Any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:44:12+00:00Added an answer on June 15, 2026 at 11:44 pm

    The reason this is happening is because when you open the file for the second time, you dont preserve the original text inside it. When you open a file and write to it in Python, you basically overwrite its contents unless you store them in a variable and re-write them.

    Try this code:

    count_line = 0
    for n, line in enumerate(open('test.txt')):
        count_line = count_line +1
        fields = line.rstrip('\n').split('\t')
        ##print count_line, fields
        already_text = open('test_words.txt').read() if n > 0 else ''
        file = open('test_words.txt', 'w+')
        count_word = 0
        file.write(already_text)
        for words in fields:
            wordsplit = words.split()
            for word in wordsplit:
                 count_word = count_word + 1
                 print count_word, word
                 file.write(str(count_word) + " " + word + '\n')
            file.close()
    

    Here’s the output when i run it:

    1 A
    2 sentence
    3 is
    4 a
    5 grammatical
    6 unit
    7 consisting
    8 of
    9 one
    10 or
    11 more
    12 words.
    1 A
    2 sentence
    3 can
    4 also
    5 be
    6 defined
    7 in
    8 orthographic
    9 terms
    10 alone.
    

    Here’s code without enumerate():

    count_line = 0
    n = 0
    for line in open('test.txt'):
        count_line = count_line +1
        fields = line.rstrip('\n').split('\t')
        ##print count_line, fields
        already_text = open('test_words.txt').read() if n > 0 else ''
        file = open('test_words.txt', 'w+')
        count_word = 0
        file.write(already_text)
        for words in fields:
            wordsplit = words.split()
            for word in wordsplit:
                 count_word = count_word + 1
                 print count_word, word
                 file.write(str(count_word) + " " + word + '\n')
            file.close()
        n += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two table s : one is the master and one the detail
Can anybody help me understand why are there two loops here? <?php // create
Hi i have notice there two date times facebook gives, one is a date
There are two parts to my question - Firstly we can attach event handlers
I just found one daunting thing. There are two versions of spring dependency coordinates.
I have read a document that they say: In java there two types of
There are two intents on the receiver side which are called from the same
There are two lists: List<string> excluded = new List<string>() { .pdf, .jpg }; List<string>
There are two project in which I collaborate, they live in different servers, A
There are two similar questions asked here and here but no adequate answers are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.