0

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!

7
  • I can't understand what you mean by the count_sentence, a string, writing "with" a file in another module. can you please explain more? Oct 24, 2022 at 19:42
  • if i print count_sentence, i will get result like total count of sentence is 4
    – lungsang
    Oct 24, 2022 at 19:47
  • please give an example with variables and the content you want to be written in the file to resolve any ambiguity. Oct 24, 2022 at 19:49
  • and I need that total count of sentence is 4 in get_para_report.csv
    – lungsang
    Oct 24, 2022 at 19:49
  • so you mean you want it to be written in the csv file instead of being printed have I understood correctly? Oct 24, 2022 at 19:50

1 Answer 1

0

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))
1
  • thank you :) I tried the first method.
    – lungsang
    Oct 24, 2022 at 20:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.