-1

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
"""
  • It's just loading the data and putting it into the list 'train_docs'. It's not working when I try the method above. – Aidan Payne Oct 08 '21 at 09:29
  • The text comes from the training document and the text is stored as doc_1, doc_2 and so on. – Aidan Payne Oct 08 '21 at 09:34
  • And now you want to create a list `[doc_1, doc_2, ...]`? – mkrieger1 Oct 08 '21 at 09:35
  • I think you are asking this: https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib – mkrieger1 Oct 08 '21 at 09:39
  • Well, instead of defining multiple variables, you could define a list to begin with. Or use `getattr` to access the variables from the imported module if you can't change it to a list. – mkrieger1 Oct 08 '21 at 09:47
  • @AidanPayne can you show the code for "training"? – Serg Zador Oct 08 '21 at 10:01

1 Answers1

-1

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

Hazza1555
  • 16
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 08 '21 at 10:33