0

I have the following structure in my dictionary:

dicc={ key:[['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP']],
key2:[['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP']],
key3:[['IP_destination','service','Port','TCP/UDP']],
key4:[['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP'],['IP_destination','service','Port','TCP/UDP']]}

And my idea is to export it to a dataframe and then export it to a csv. And I want it to look something like this:

enter image description here

Guy
  • 46,488
  • 10
  • 44
  • 88
  • This is a duplicate of [this](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) and [this](https://stackoverflow.com/questions/16923281/writing-a-pandas-dataframe-to-csv-file) – user42 Aug 18 '22 at 09:25

1 Answers1

1

The values in your dicc don't match the values present in the table from the picture.

The table shown in the picture/excel correspond to a different dictionnary structure, this one :

dicc = {'keys': ['Key', 'key1', 'key2', 'key3'],
 'lp_dest': ['lp_dest1\nlp_dest2\nlp_dest3','lp_dest1\nlp_dest2','lp_dest1','lp_dest1\nlp_dest3\nlp_dest4\nlp_dest5\nlp_dest6'],
 'service': ['net-bios\nhttps\nssh','net-bios\nhttps\nssh','net-bios\nhttps\nssh','net-bios\nhttps\nssh'],
 'port': ['22\n443\n475','22\n443\n476', '22\n443\n477', '22\n443\n478'],
 'tcp/udp': ['TCP', 'UDP', 'UDP', 'TCP']}

Now if you're looking for a way of how to convert a dict. to a df/csv, you can try the following :

pd.DataFrame(dicc).to_csv('YourCsvName.csv', index=False)
Timeless
  • 22,580
  • 4
  • 12
  • 30
  • Good morning, OK, I understand, but I have to start from this dictionary scheme. Is there any way to reconvert it to your dictionary scheme? If so, could you help me because I'm already a bit stuck and I'm new to programming and I don't have much of an idea. Greetings and thank you very much in advance! – Martin Garcia Quinde Aug 19 '22 at 10:17