1

I'm trying to write the result of a function in a csv. Unfortunately, no pandas.

csv file input:

Hello all well?
today is cold!
I have not had lunch yet
He does not have many brothers or sisters.
We are sick

Script:

import re
import csv
import string

with open('teste_csv.csv', 'r') as f:
    file = csv.reader(f)

    for line in file:
        message = ''.join(line)

        def toto(message):

            message = message.lower()

            p = re.compile('|'.join(map(re.escape, string.punctuation)))
            no_punct = p.sub(' ', message)

            writer = csv.writer(open('result.csv', 'w'))
            for row in no_punct:
                writer.writerow(row)

                return writer

         print(toto(message))

At my terminal, I have <_csv.writer object at 0x7fee60e57c50> and in my result.csv I have only one line written 'w'. I would like each line to be in my result.csv

3
  • 2
    Just separate this to three fucntions that read, process and write back to file, and a little puppy will not die when you run that code. Commented Jul 26, 2018 at 11:44
  • 2
    This is not a csv file. Why do you want to use the csv module on that??? Commented Jul 26, 2018 at 11:57
  • I agree with you, but I have no other choice... Commented Jul 26, 2018 at 12:19

2 Answers 2

3

You keep erasing the file since everytime you call toto it opens result.csv for writing, hence you are left only with a single write. You need to open the file once ,and create the wirter once. You also only need to define the function once for that matter:

import re
import csv
import string

def toto(message,writer):
    message = message.lower()
    p = re.compile('|'.join(map(re.escape, string.punctuation)))
    no_punct = p.sub(' ', message)
    for row in no_punct:
        writer.writerow(row)

with open('teste_csv.csv', 'r') as f:
    writer = csv.writer(open('result.csv','w'))
    file = csv.reader(f)
    for line in file:
        message = ''.join(line)
        toto(message,writer)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to put the writer outside of your first loop. each time you are looping throw it's opening and rewriting the file

another issue you are defining and calling the toto inside the loop so it's getting called with last message value.

import re
import csv
import string

with open('test.csv', 'r') as f:
    file = csv.reader(f)
    writer = csv.writer(open('result.csv', 'w'))
    def toto(message):

        message = message.lower()
        p = re.compile('|'.join(map(re.escape, string.punctuation)))
        no_punct = p.sub(' ', message)
        for row in no_punct:
            writer.writerow(row)
            return writer     
    for line in file:
        print line
        message=''.join(line)

    print(toto(message))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.