0

Is there anyway that I can retrieved my config file content and display the necessary content in a logical format using for loop?

[_1_ubu]
control_name = 1.1 Ensure AppArmor is installed
outcome = PASS
sys_out = Status: install ok installed
expected_out = AppArmor is installed
remediation = apt install apparmor
severity = MEDIUM

[_2_ubu]
control_name = 1.2 Ensure Avahi Server is not installed
outcome = FAIL
sys_out = Status: install ok installed
expected_out = avahi-daemon is installed
remediation = apt purge avahi-daemon
severity = MEDIUM

[_3_ubu]
control_name = 1.3 Ensure CUPS is not installed
outcome = PASS
sys_out = not installed
expected_out = cups is not installed
remediation = apt purge cups
severity = MEDIUM

In a structure like this:

Screenshot of desired results

martineau
  • 119,623
  • 25
  • 170
  • 301
Ahsu21
  • 21
  • 4
  • 2
    Yes, there is a way. – MattDMo Jul 08 '22 at 13:53
  • You could use `configparser` and `csv`. Setup a CSV DictWriter. Then use the config parser's `section` method to enumerate sections and its `items` method to read each section's options. Turn the items into a dict and pass to the writer. – tdelaney Jul 08 '22 at 14:06
  • You could do it by using the [`configparser`](https://docs.python.org/3/library/configparser.html#module-configparser) module to read the INI data and the [`csv`](https://docs.python.org/3/library/csv.html#module-csv) module to write it out in CSV format. – martineau Jul 08 '22 at 14:06
  • My suggestion assumes that the entire INI follows the pattern given. It would not be a general ini-to-csv solution because most INI's have a different structure per section. – tdelaney Jul 08 '22 at 14:08
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 08 '22 at 15:10

2 Answers2

1

You can try this example to read the .ini file and write it to .csv file:

import pandas as pd
import configparser

config = configparser.ConfigParser()
config.read("input.ini")

df = pd.DataFrame([config[s] for s in config.sections()])
print(df)

df.to_csv("result.csv", index="False")

Prints:

                               control_name               expected_out outcome             remediation severity                       sys_out
0          1.1 Ensure AppArmor is installed      AppArmor is installed    PASS    apt install apparmor   MEDIUM  Status: install ok installed
1  1.2 Ensure Avahi Server is not installed  avahi-daemon is installed    FAIL  apt purge avahi-daemon   MEDIUM  Status: install ok installed
2          1.3 Ensure CUPS is not installed      cups is not installed    PASS          apt purge cups   MEDIUM                 not installed

and saves result.csv.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • You're a savior it works like about what i was intending. Can you please explain how does the structure works as ive not used pandas library. I can't believe that it only takes few lines of coding to achieve that format. Also, if i want the header(control_name, outcome) to be bold, how do i achieve that – Ahsu21 Jul 10 '22 at 11:46
0

Once you can have it in a pandas dataframe it would only be necessary to save it as ".csv", look at these answers similar to your question:

How to read and write INI file with Python3?