PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 24, 2022

[FIXED] how to write output of one module in another module as csv file

 November 24, 2022     export, import, module, python, python-3.x     No comments   

Issue

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!


Solution

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))


Answered By - Dariush Mazlumi
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing