I have to append first 3 sentences of this sample text to a file(data.txt). So far I've done this:

import nltk

text="""First patented in 1876 by Alexander Graham Bell and further developed by many others, the telephone was the first device in history that enabled people to talk directly with each other across large distances. Telephones became rapidly indispensable to businesses, government, and households, and are today some of the most widely used small appliances.
The essential elements of a telephone are a microphone (transmitter) to speak into and an earphone (receiver) which reproduces the voice of the distant person. In addition, most telephones contain a ringer which produces a sound to announce an incoming telephone call, and a dial used to enter a telephone number when initiating a call to another telephone. Until approximately the 1970s most telephones used a rotary dial, which was superseded by the modern Touch-Tone push-button dial, first introduced by AT&T in 1963."""

sentences = nltk.sent_tokenize(text)
for sent in sentences:
    for x in range(3):
        with open('data.txt', 'a') as f:
            f.write('{}\n'.format(sent))

But it gives the wrong result (repeats sentences). What am I doing wrong?

Try this ...

''' nltk_sentences101.py

downloaded and installed:
nltk-2.0.4.win32.exe
from:
https://pypi.python.org/pypi/nltk

tested with Python27
'''

import nltk
import pprint

text="""First patented in 1876 by Alexander Graham Bell and further developed by many others, the telephone was the first device in history that enabled people to talk directly with each other across large distances. Telephones became rapidly indispensable to businesses, government, and households, and are today some of the most widely used small appliances.
The essential elements of a telephone are a microphone (transmitter) to speak into and an earphone (receiver) which reproduces the voice of the distant person. In addition, most telephones contain a ringer which produces a sound to announce an incoming telephone call, and a dial used to enter a telephone number when initiating a call to another telephone. Until approximately the 1970s most telephones used a rotary dial, which was superseded by the modern Touch-Tone push-button dial, first introduced by AT&T in 1963."""

sentences = nltk.sent_tokenize(text)

pprint.pprint(sentences)  # test

# write the first three sentences to file
num_sentences = 3
with open('aa_data.txt', 'w') as fout:
    for sentence in sentences[:num_sentences]:
        print(sentence)
        fout.write(sentence+'\n')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.