简体   繁体   中英

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

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:

所需结果的屏幕截图

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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM