Generate output in csv from a code in python

3

I hope you can help. I'm new to python and my question is this. I created a regex that extracts from a file in csv the parts of the text that I want. But I want to save the output of this function in csv to compare with the start table to know how my regex is good. Basically I get the output of my function and generate a table in csv . Can someone help me? Thanks!

Follow the code below. What I would like to do is save the pms list in csv along with some columns from the input table (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 for not past! I'm a newbie. Thanks!

    
asked by anonymous 17.10.2016 / 14:43

1 answer

1

To save to CSV, you can use the built-in Python library: link

More specifically, the .write() method:

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
    
19.10.2016 / 15:57