Home > Software design >  Calling text from another file in python
Calling text from another file in python

Time:10-08

I have some text from another python file and would like to call it into the file I'm working on, where I have labelled each section of the text doc_1, doc_2,..., doc_13. I have successfully imported the file, where the file is called training.

Here is the code:

from documents import training as train # imported the file

doc_labels = []
for i in range(1, 14):
    doc = f'doc_{i}'
    doc_labels.append(doc)

print(doc_labels) # This prints out a list of the document names

train_docs = []
for i in range(len(doc_labels)):
    doc = train.doc_labels[i] # trying to call the text from the documents
    train_docs.append(doc)

Here is the list of documents with the text, this is the training file:

doc_1 = """
text
"""

doc_2 = """
text
"""

.
.
.

doc_13 = """
text
"""

CodePudding user response:

Can you add a seperator to the document and use str.split(separator)

or use Regex101.com to work out what kinda match you want to do.

or check this answer answer

  • Related