Referencing INI files in Python or Ruby

Introduction

It is a method to read the INI file as an example of loading the settings that went out with Ruby and Python.

Execution environment

Python 2.7.5 Ruby 1.9.3

INI file

The INI file to be read looks like this. "Hoge3" is not described in "TEST2" so that you can see the behavior when there is no key name.

test.ini


[TEST1]
hoge1=aaa
hoge2=bbb
hoge3=ccc
hoge4=ddd

[TEST2]
hoge1=AAA
hoge2=BBB
hoge4=CCC

Python First, let Python load the configuration file. Python uses the standard "ConfigParser" module. The execution environment is 2.7, but this area should be almost the same even in 3 series. .. ..

readSetting.py




import ConfigParser

def _get(inifile, section, name):
    try:
        return inifile.get(section, name)
    except Exception, e:
        return "error: could not read " + name

if __name__=="__main__":

    inifile = ConfigParser.SafeConfigParser()
    inifile.read("./test.ini")

    for section in inifile.sections():
        print '===' + section + '==='
        print _get(inifile, section, "hoge1")
        print _get(inifile, section, "hoge2")
        print _get(inifile, section, "hoge3")
        print _get(inifile, section, "hoge4")

result
===TEST1===
aaa
bbb
ccc
ddd
===TEST2===
AAA
BBB
error: could not read hoge3
CCC

If there is no key name, it seems to throw an error.

Ruby Then do the same in Ruby. The execution environment is 1.9.3. In the case of Ruby, you need to install the "inifile module" first. To install, just execute the following gem command.

gem install inifile

And the source code.

readSetting.rb



require 'inifile'

def _get(inifile, section, name)
  begin
    return inifile[section][name]
  rescue => e
    return "error: could not read #{name}"
  end
end

inifile = IniFile.load('./test.ini')

inifile.each_section do |section|
  puts "===#{section}==="
  puts _get(inifile, section, 'hoge1')
  puts _get(inifile, section, 'hoge2')
  puts _get(inifile, section, 'hoge3')
  puts _get(inifile, section, 'hoge4')
end
result
===TEST1===
aaa
bbb
ccc
ddd
===TEST2===
AAA
BBB

CCC

In the case of ruby, it seems that an empty string is returned when there is no key name.

Summary

Python Module is provided as standard Throw an error when there is no key name

Ruby Requires installation If there is no key name, an empty string will be returned and no error will occur.

A little impression

It seems that the application is better if you do not throw an error even if you do not have a key name, I'm happy with the standard if it's an environment where you can't install it freely.

Recommended Posts

Referencing INI files in Python or Ruby
Transpose CSV files in Python Part 1
Manipulate files and folders in Python
Handling of JSON files in Python
Download Google Drive files in Python
Sort large text files in Python
Read files in parallel with Python
Export and output files in Python
Extract strings from files in Python
[AWS] Using ini files with Lambda [Python]
Differences between Ruby and Python in scope
Find files like find on linux in Python
Output tree structure of files in Python
Express Python yield in JavaScript or Java
OR the List in Python (zip function)
Type annotations for Python2 in stub files!
Automate jobs by manipulating files in Python
Read and write JSON files in Python
Sample for handling eml files in Python
Non-logical operator usage of or in python
Download files in any format using Python
Try something like Python for-else in Ruby
How to write Ruby to_s in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
[Python] Get the files in a folder with Python
Meta-analysis in Python
Unittest in python
Summary of how to import files in Python 3
Resolve Japanese write error UnicodeEncodeError in Python files
Epoch in Python
Discord in Python
GNU GLOBAL (gtags) + α in Go, Ruby, Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Reading and writing CSV and JSON files in Python
N-Gram in Python
Programming in python
Handle zip files with Japanese filenames in Python 3
Plink in Python
Constant in python
Character encoding when dealing with files in Python 3
Split files when writing vim plugin in python
How to get the files in the [Python] folder
Lifegame in Python.
FizzBuzz in Python
Rock-paper-scissors in Ruby
Sqlite in python
StepAIC in Python
Get files, functions, line numbers running in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python