python import config file code example

Example 1: how to use config file to remove redundant variables in code

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('simple.ini')

print parser.get('bug_tracker', 'url')

Example 2: cant import config python 3

/anaconda2/lib/python2.7/site-packages/boto3/session.py in <module>()
     16 
     17 import botocore.session
---> 18 from botocore.client import Config
     19 from botocore.exceptions import DataNotFoundError, UnknownServiceError
     20 

ImportError: cannot import name Config

Example 3: python config file json datatypes

import pandas as pd
import json
import numpy as np

# Create a file (csv) for test purposes 
data = '''\
3,10,3.5,abc
3,010,3,bcd'''
file = io.StringIO(data)

# Create a file (json) for test purposes
json_data = '''\
{
   "header":[
       ["field1","int16"],
       ["field2","float32"],
       ["field3","float64"],
       ["field4","str"]]
}'''

# Load json to dictionary
json_d = json.loads(json_data)

# Fetch field names and dtypes
names = [i[0] for i in json_d['header']]
dtype = dict(json_d['header'])

# Now use pandas to read the whole thing to a dataframe
df = pd.read_csv(file,header=None,names=names,dtype=dtype)

# Output as dict (this can be passed to a json file with json.dump())
df.to_dict('r')

Tags:

Misc Example