Word processing in csv file

Asked

Viewed 666 times

0

I have a database with over a thousand opinions (text) in a csv file, however I need only texts that contain more than 5 words to stay in my file. The problem is to make a code that goes through the entire document line by line and leaves only opinions that is more than 5 words, I have already researched some libraries and did not succeed in this implementation.

I’m using this code to read the file:

import csv
lines = []
finalLines = []

 with open('aborto.csv') as csvfile:
   readerCSV = csv.reader(csvfile, delimiter=',')
   print(readerCSV)

 for row in readerCSV:
    print(row) 

Entree: inserir a descrição da imagem aqui Exit: inserir a descrição da imagem aqui

  • What is the organization of the file? Each line is an opinion? What if there are commas within the text?

  • Exactly this my problem, as commas are delimiters I’m confused how to proceed with code.

  • Okay, but did you create a CSV, which uses the comma as the delimiter, to store comma-bearing texts? Have to [Edit] the question and add a snippet from your CSV file?

  • I added a photo of the terminal when I run my code

  • I think you got it wrong. Add an excerpt from the file CSV which will be read by Python.

  • See if it’s correct.

Show 1 more comment

1 answer

1


Basically you need to fix the indentations of your code. See an example already using the CSV writing process:

import csv

# Abre o arquivo de saída para escrita:
with open("data.csv", 'r') as file_read, open('output.csv', 'w') as file_write:

    # Define o leitor CSV do arquivo:
    reader = csv.reader(file_read, delimiter=' ')

    # Define o escritor CSV do arquivo:
    writer = csv.writer(file_write, delimiter=' ')

    # Percorre as linhas do arquivo de entrada:
    for row in reader:

        # Verifica se o tamanho da linha é maior que o desejado:
        if len(row) > 5:

            # Escreve a linha no arquivo de saída:
            writer.writerow(row)
  • I redid the code with its representation, more when it arrives in a certain line (line 117) to rewrite in the output file.

  • @Rivaldohater changes the delimiter to a blank space, as I did after editing the answer.

  • It worked perfectly, thank you.

Browser other questions tagged

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