Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python - Limit Sentence Length to 10 Words - Text file
I am trying to limit each sentence to 10 words

file = open("a.txt","r") 
content =

file_out = open("out.txt","w+") 

for i in range(len(content)):


    if (i + 1).join(i.split()[:10]):


what would be the correct syntax?


Python newbie trying to learn the ropes
Try to use the context manage with when manipulate files.

Check if this is what you want:
with open("a.txt", "r") as f_in:
    with open("out.txt", "w+") as f_out:
        for line in f_in:
            words = line.split(' ')[:10]
            f_out.write(' '.join(words) + '\n')
dj99 likes this post
Thank you GJ,

that did the trick

Have a great weekend!


Python newbie trying to learn the ropes

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  auto supply values to a python script(function) from text file metro17 4 163 Oct-26-2019, 01:25 AM
Last Post: ichabod801
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 205 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Voynich search engine in python using dashes & dot totals to find Italian words Pleiades 3 191 Oct-10-2019, 10:04 PM
Last Post: Pleiades
  Open and read multiple text files and match words kozaizsvemira 2 229 Sep-11-2019, 12:58 PM
Last Post: kozaizsvemira
  Trying to make column based file from text file scor1pion 7 365 Jul-16-2019, 02:43 PM
Last Post: scor1pion
  How best to format text in a simple text file? Pedroski55 2 622 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 298 Jun-13-2019, 07:02 AM
Last Post: perfringo
  Counting words in the last line of a file Drone4four 15 1,125 May-31-2019, 09:30 PM
Last Post: snippsat
  Print to Text file in Python 3 iteachpc 2 418 May-02-2019, 04:38 AM
Last Post: perfringo
  Create a function to find words of certain length ag4g 2 440 Apr-21-2019, 06:20 PM
Last Post: BillMcEnaney

Forum Jump:

Users browsing this thread: 1 Guest(s)