Home > database >  Python: Trying to print fields of config.ini to csv file
Python: Trying to print fields of config.ini to csv file

Time:03-03

I have a config.ini file which the user should supply certain fields they want to show as headers in a CSV file. I have tried a few different methods but cannot seem to get what I need printed.

This is what the INI file looks like: This is a picture of my config.ini file.

import csv
from dataclasses import fields
from distutils.command.config import config
import pandas as pd
from jira import JIRA
from atlassian import Confluence
from configparser import ConfigParser


csvFilename = "csvHeaders.csv" #File name

## Config file
config_file = ConfigParser() #create/get configparser object
config_file.read("config.ini")

#Get the fields from config file
#fieldlist = []
fieldlist = config_file["FIELDINFO"]
#print(fieldlist['fields'])
#print (fieldlist)                           #1ST PRINT STATEMENT -- This prints <Section:FIELDINFO>
#with open ('config.ini', 'w') as conf:
#    config_object.write(conf)

for i in fieldlist:
    #print(fieldlist[i])
    #csvHeaders = fieldlist['fields']
    csvHeaders = fieldlist[i]
    #print(csvHeaders)                       #2ND PRINT STATEMENT -- Prints list of fields in config.ini

with open(csvFilename, 'w') as f:
    csvwriter = csv.writer(f)
    #csvwriter.writerow(fieldlist[i])
    #csvwriter.writerow(csvHeaders)
    #csvwriter.writerow(fieldlist['fields'])
    csvwriter.writerow(fieldlist)
    #for i in (fieldlist):
    #    csvwriter.writerow(fieldlist[i])

Currently this prints out, field1,field2,field3 etc rather than the field name. When I try to manipulate the writerow statement to fieldlist[i], it prints the field names but with commas in between each character. What am I doing wrong? Thank you.

CodePudding user response:

You can try this code.

import csv
from configparser import ConfigParser

csvFilename = "csvHeaders.csv" #File name


# Config file
config_file = ConfigParser() #create/get configparser object
config_file.read("config.ini")

#Get the list 
fieldlist = config_file["FIELDINFO"]

#create headers
csvHeaders = []
#copy the field names into an arry
for i in fieldlist:
  csvHeaders.append(fieldlist[i])

with open(csvFilename, 'w', newline='') as f:
    csvwriter = csv.DictWriter(f, fieldnames=csvHeaders)
    #write header
    csvwriter.writeheader()
    #write a sample row.
    csvwriter.writerow({csvHeaders[0]:'1',
                        csvHeaders[1]:'2',
                        csvHeaders[2]:'3',
                        csvHeaders[3]:'4',
                        csvHeaders[4]:'5',
                        csvHeaders[5]:'6',
                        csvHeaders[6]:'7'}
    )

Refer csv file documentation

CodePudding user response:

To create a CSV header from the field values in config.ini, you can do the following:

import csv
from configparser import ConfigParser

csvFilename = "csvHeaders.csv" #File name

## Config file
config_file = ConfigParser() #create/get configparser object
config_file.read("config.ini")
fieldlist = config_file["FIELDINFO"]

with open(csvFilename, 'w', newline='') as f:
    csvwriter = csv.writer(f)
    csvwriter.writerow(fieldlist.values())

This would create a normal CSV file header as:

Issue Key,Status,Created,Updated,Priority,Summary,Type

Note: A CSV file by default will have commas separating each column (which is why the are called Comma Separated Variable files). You can change the delimiter being used but the default should normally be used.

If one of your fields in the INI file happens to contain a comma, the CSV library will automatically wrap the entry in quotes for you which is needed to allow a program reading the file back in to work correctly.

  • Related