Home > Software engineering >  how to write output of one module in another module as csv file
how to write output of one module in another module as csv file

Time:10-25

I am trying to write output of one module in a output file of another module. For example,

first module :

def sentence_token():
    
    # some code
    
    sentences = sentence_tokenizer(tokens)
    count_sentence = f"total count of sentence is {len(sentences)}"

second module :

def word_pos_definition():
    
    # some code
    word_pos_definition_content = []
    with open('get_para_report.csv', 'w') as final_csv_file:
        final_csv_file.write('\n'.join(word_pos_definition_content))

I need the count_sentence in first module write with get_para_report.csv in second module. Anyone have any idea, would help alot. Thanks!

CodePudding user response:

you can do multiple things to have both values in your CSV file.

  1. you can append your data, so the content another file has made doesn't get deleted.
def sentence_token():
    
    # some code
    sentences = sentence_tokenizer(tokens)
    count_sentence = len(sentences)}

    with open('file.csv', 'a') as file:
        file.write(str(count_sentences)) # a \n might be desired depending on your exact output

def word_pos_definition():
    # some code
    word_pos_definition_content = []

    with open('file.csv', 'w') as file:
        file.write('\n'.join(word_pos_definition_content))
  1. you can import any module in another, or maybe a third main module who gets the values from both modules and does the write operation
def sentence_token():
    
    # some code
    
    sentences = sentence_tokenizer(tokens)
    count_sentence = len(sentences)

    return count_sentence
import first_module

def word_pos_definition():
    # some code
    word_pos_definition_content = []
    count_sentence = first_module.sentence_token()

    with open('file.csv', 'w') as file:
       file.write((str(count_sentence) '\n').join(word_pos_definition_content))
  • Related