Home > Blockchain >  How to transfer content from config.ini file to csv file in a logical format?
How to transfer content from config.ini file to csv file in a logical format?

Time:07-10

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

CodePudding user response:

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.

CodePudding user response:

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?

  • Related