Formatting of Python File

Asked

Viewed 68 times

0

Hello, I’m a beginner in Python and I’m working with natural language processing. When processing a text and separating the sentences, I want to write this way in the << SENTENCE >> << SENTENCE >> ... But I don’t know how to do this, I know how to print on screen, but not how to write to the file in this format. If anyone can give me an El thank you very much

import nltk
import nltk.data


arquivo = open('base_setenciado.txt', 'w')

raw_text = open('base.txt').read()

sent_tokenizer=nltk.data.load('tokenizers/punkt/portuguese.pickle')

sentences = sent_tokenizer.tokenize(raw_text)

for sent in sentences[500:505]:

    print("<<", sent, ">>\n")

1 answer

1


If you’re used to using print() can pass your file as argument file for print() that he will print the file instead of the sys.stdout as usual.

Ex.:

import nltk
import nltk.data

raw_text = open('base.txt').read()
sent_tokenizer=nltk.data.load('tokenizers/punkt/portuguese.pickle')
sentences = sent_tokenizer.tokenize(raw_text)

with open('base_setenciado.txt', 'w') as arquivo:
    for sent in sentences[500:505]:
        print("<<", sent, ">>\n", file=arquivo)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.