Generate csv output of python code

Asked

Viewed 5,659 times

4

I hope you can help. I’m new to python and my question is this. I created a regex extracted from a file in csv the parts of the text I want. But I want to save the output of this function in csv to compare with the starting table to know how mine regex is good. Basically we take the output of my function and generate a table in csv. Can someone help me? Thank you!

Below is the code. What I would like to do is to save the pms list in csv along with some input table columns(df)

import re
import pandas as pd

df = pd.read_csv('C:/Users/b223902594/Documents/Sentenças/codigos/teste.csv', sep=';')

sentenca = df["descricao"] #descrição é uma das quatro colunas da tabela original.

def verificar_padroes_sentenca(lista_sentencas):
regex = r'(julgo .* dias-multa)|(julgo.*\babsolv)|(julgo .* punibilidade)|    (julgo .* Lei)| (julgo .* dias multa)'
for sentenca in lista_sentencas:
pms = re.findall(regex, sentenca, flags=re.IGNORECASE)
print(pms)

Sorry I didn’t pass before! I’m new. Grateful!

1 answer

1

To save to CSV, you can use the built-in Python library: https://docs.python.org/3/library/csv.html

More specifically the method .write():

import csv
# abrindo o arquivo para escrita
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) # escreve tres strings no csv

Browser other questions tagged

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