Home > Mobile >  pass the print function outcome into a list
pass the print function outcome into a list

Time:08-24

I am experimenting with MISP taxonomies

!git clone https://github.com/MISP/PyTaxonomies
cd PyTaxonomies
!git submodule init && git submodule update
!python3 setup.py install
from pytaxonomies import Taxonomies
taxonomies = Taxonomies()

What I want is to pass the print outcome inside a list

for i in taxonomies.values():
    print (i)

so I am trying the following:

taxonomies = []

for i in taxonomies.values():
    taxonomies.append(i)

taxonomies

However, I get back

[<pytaxonomies.api.Taxonomy at 0x7f67b1d86250>,
 <pytaxonomies.api.Taxonomy at 0x7f67ad048590>,
 <pytaxonomies.api.Taxonomy at 0x7f67b14f7b10>,
 <pytaxonomies.api.Taxonomy at 0x7f67b14f7550>,
.......
.......
 <pytaxonomies.api.Taxonomy at 0x7f67ac805290>,
 <pytaxonomies.api.Taxonomy at 0x7f67ac8052d0>,
 <pytaxonomies.api.Taxonomy at 0x7f67ac7ecc50>]

What I was expected to get back is the following:

[CERT-XLM:abusive-content="spam"
CERT-XLM:abusive-content="harmful-speech"
CERT-XLM:abusive-content="violence"
CERT-XLM:malicious-code="virus"
CERT-XLM:malicious-code="worm"
....
....
workflow:state="incomplete"
workflow:state="complete"
workflow:state="draft"
workflow:state="ongoing"
workflow:state="rejected"]

Any ideas to solve my problem?

CodePudding user response:

First of all be careful with the names of your variables, you repeated taxonomies tice which might destroy any data in your variable. For your question, that is pretty normal giving that the list's values types are 'taxonomy' types, you just have to cast them to a string before. Here's a little example (I also changed a it a little using list comprehension to get a shorter version of the code) :

from pytaxonomies import Taxonomies

# fetching taxonomies
taxonomies = Taxonomies()

# casts the taxonomies output as a string before saving it in a list
taxonomies_out = [str(tax).replace('\n', ' ') for tax in taxonomies.values()]

Hope that helps.

  • Related