PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

[FIXED] How to convert an INI file into an CSV file

 August 26, 2022     bash, csv, export-to-csv, ini, python     No comments   

Issue

I would like to create a csv from a list of data, but the key values differ between sections of the list. The list is of the following layout:

[Game 1]
Publisher=
Developer=
Released=Nov, 2005
Systems=
Genre=Action|Strategy
Perspective=3rd-Person Perspective
Score=4.5
Controls=
Players=
Rating=
Url=http://www.google.com.pl
Description=This cartridge contains six of the 1 kilobyte e......

[Game 2]
Publisher=Home Entertainment Suppliers Pty. Ltd.
Developer=Imagic
Released=1992
Systems=
Genre=Action
Perspective=3rd-Person Perspective
Score=1.5
Controls=Joystick (Digital)|Same/Split-Screen Multiplayer
Players=1-2 Players
Rating=
Url=http://www.google.com
Description=An unlicensed multi-cart from the Australian-bas.....
Goodname=2 Pak Special - Alien Force & Hoppy
NoIntro=
Tosec=2 Pak Special Light Green - Hoppy & Alien Force

Full file here

Each set of data is separated by [Game *] and the values presented for each game can be blank or non existent for some games, eg Goodname=, NoIntro= and Tosec= are missing from Game 1. I don't know the total number of keys/columns required. Ideally I would like each game on a separate line in the csv file.

Anyone have any ideas on how to get this format of data into a csv? I'm stumped. I'm familiar with bash and python but I'm open to any suggestions of how to automate the conversion.

Thanks in advance.


Solution

In Python you can use the ConfigParser library for reading the INI file and the csv library for writing a comma separated file. I wrote below a small script ini2csv.py that you can use to handle your conversion using the following command:

cat atari.ini | ./ini2csv.py > atari.csv

Here is the script:

#!/usr/bin/python
# encoding: utf-8

import sys
import csv
from ConfigParser import ConfigParser

ini = ConfigParser()
ini.readfp(sys.stdin)

#Find all keys in the INI file to build a row template and 
#include a "game" field to store the section name.
rowTemplate = {"game":""} 
for sec in ini.sections():
   for key,value in ini.items(sec):
       rowTemplate[key] = "" 

#Write the CSV file to stdout with all fields in the first line
out = csv.writer(sys.stdout)
out = csv.DictWriter(sys.stdout, fieldnames=rowTemplate.keys())
out.writeheader()

#Write all rows
for sec in ini.sections():
   row = rowTemplate.copy()
   row["game"] = sec
   for key,value in ini.items(sec):
       row[key] = value
   out.writerow(row)

I tested it with the link you provided in your question and it appears to work as expected.



Answered By - Arton Dorneles
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing