-
Notifications
You must be signed in to change notification settings - Fork 44
/
Chapter_18th.py
113 lines (93 loc) · 2.49 KB
/
Chapter_18th.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# ******************* Execute Examples in terminal ********************* #
# Example 1
#####################
### Read INI file ###
#####################
import configparser
config = configparser.ConfigParser()
# give the correct path of spyder.ini file
config.read("C:\\Users\sana.rasheed\.spyder-py3\config\spyder.ini") #("C:\\Users\.spyder-py3\spyder.ini")
config.sections()
print( config.sections() )
# Output
# ['main',
# 'internal_console',
# 'main_interpreter',
# 'ipython_console',
# 'variable_explorer',
# 'plots',
# 'editor',
# 'historylog',
# 'help',
# 'onlinehelp',
# 'outline_explorer',
# 'project_explorer',
# 'explorer',
# 'find_in_files',
# 'breakpoints',
# 'profiler',
# 'pylint',
# 'workingdir',
# 'shortcuts',
# 'appearance',
# 'lsp-server',
# 'fallback-completions',
# 'kite']
[option for option in config['help']]
print( [option for option in config['help']] )
# Output
# ['enable',
# 'max_history_entries',
# 'wrap',
# 'connect/editor',
# 'connect/ipython_console',
# 'math',
# 'automatic_import',
# 'rich_mode',
# 'first_time']
# Example 2
##########################
### Create Dictionary ###
#########################
parser = configparser.ConfigParser()
parser.read_dict(
{'section1':
{'tag1': '1','tag2': '2','tag3': '3'},
'section2':
{'tagA': 'A','tagB': 'B','tagC': 'C'},
'section3':
{'foo': 'x','bar': 'y','baz': 'z'} })
parser.sections() #['section1', 'section2', 'section3']
print(parser.sections())
# Output
# ['section1', 'section2', 'section3']
[option for option in parser['section1']]
print([option for option in parser['section1']])
# Output
# 'tag1', 'tag2', 'tag3'
# Example 3
##############################
### Read Text/String file ###
#############################
sample_config = """
[My Settings]
user = username
profile = /my/directory/to/profile.png
gender = male
[My new Settings]
user = Sana.Rasheed
profile = /my/directory/to/profile.png
gender = female
"""
# creates an instance of ConfigParser called config
config = configparser.ConfigParser()
#read the instance of string using read_string() method
config.read_string(sample_config)
config.sections() #['My Settings']
print(config.sections())
# Output
# ['My Settings', 'My new Settings']
user = config["My new Settings"]["user"] #'username'
print(user)
# Output
# 'Sana.rasheed'