简体   繁体   English

如何将 INI 文件转换为 CSV 文件

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

I would like to create a csv from a list of data, but the key values differ between sections of the list.我想从数据列表中创建一个 csv,但列表各部分之间的键值不同。 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.每组数据由 [Game *] 分隔,并且对于某些游戏,为每个游戏呈现的值可能为空白或不存在,例如,游戏 1 中缺少 Goodname=、NoIntro= 和 Tosec=。我不知道总数所需的键/列数。 Ideally I would like each game on a separate line in the csv file.理想情况下,我希望每个游戏在 csv 文件中单独一行。

Anyone have any ideas on how to get this format of data into a csv?有人对如何将这种格式的数据转换为 csv 有任何想法吗? I'm stumped.我难住了。 I'm familiar with bash and python but I'm open to any suggestions of how to automate the conversion.我熟悉 bash 和 python 但我愿意接受有关如何自动转换的任何建议。

Thanks in advance.提前致谢。

In Python you can use the ConfigParser library for reading the INI file and the csv library for writing a comma separated file.在 Python 中,您可以使用ConfigParser库来读取INI 文件,并使用csv库来编写逗号分隔文件。 I wrote below a small script ini2csv.py that you can use to handle your conversion using the following command:我在下面写了一个小脚本ini2csv.py ,您可以使用以下命令来处理您的转换:

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.我使用您在问题中提供的链接对其进行了测试,它似乎按预期工作。

Are you sure you need verbose python?你确定你需要详细的 python 吗? Perl goes quicker... Perl 走得更快...

 #! /usr/bin/perl
 my $c = "None" ;                   # current category
 while(<>) {                        # parse diamond operator (STDIN+...)
   next if m/^[;\#]/ || m/^$/ ;     # skip comments and empty lines
   if (m/^\[([^\[])\]/) { $c = $1 } # switch current category 
   else { "$c\t$_"; }               # print with tabulation
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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