It is easier to read the configuration file using python, for example, the following configuration file:
0. ini file:
--------------- File start ----------------
[Global]
IP = 192.168.1.100; IP Address
Port = 1234
MAC = 0x414243444546; Mac
--------------- End of file ----------------
You can use the followingCode:
1 # ! /Usr/bin/Python
2 # -*-Coding: UTF-8 -*-
3 Import Configparser
4
5 Config = configparser. configparser ()
6 Config. readfp (open (raw_input ( " Input File Name: " ), " RB " ))
7
8 Print Config. Get ( " Global " ," IP " )
Running result:
>>>
Input File Name: 0.ini
192.168.1.100
If you change the last line to print config. Get ("Global", "Mac ")
Running result:
>>>
Input File Name: 0.ini
0x414243444546; Mac
However, the data I read is not what I expected (mainly because the INI file is incorrectly written). Let's look at the configuration file I defined below:
1. ini file:
--------------- File start ----------------
# Configuration file
<Part1>
[Global] # global configuration parameters
IP = 192.168.1.100
Port = 1234
MAC = 0x414243444546 # MAC address
<Part2>
[Global] # global configuration parameters
IP = 192.168.1.101
Port = 1234
MAC = 0x414243444547 # MAC address
--------------- End of file ----------------
The above Code cannot be parsed for the extended configuration file format. The default configuration is sometimes too limited and not easy to use.
We can use the python dict data structure to parse the configuration file, which is also very convenient to use.
The following is an example to read the above extension configuration file.
Sample Code for reading:
1 # ! /Usr/bin/Python
2 # -*-Coding: UTF-8 -*-
3
4 Partlable = ( " < " ," > " )
5 Sectionlable = ( " [ " , " ] " )
6 Endlinelable = " \ R \ n " # Line flag in Windows
7 # Endlinelable = "\ n" # Line flag in Linux
8 Equallable = " = " # Value assignment mark
9 Notelable = ' # ' # Annotation mark
10
11 # Get the map of the total Configuration
12 Def Getplatformmap (strtmp, lable1 = partlable, lable2 = sectionlable ):
13 TMP = strtmp. Split (lable1 [0])
14 TMP = [ELEM For ELEMIn TMP If Len (ELEM)> 1]
15 TMP = [ELEM For ELEM In TMP If ELEM. rfind (lable1 [1])> 0]
16 Platdict = {}
17 For ELEM In TMP:
18 Key = ELEM [0: ELEM. Find (lable1 [1]):]
19 Value = ELEM [ELEM. Find (lable2 [0]):]
20 Platdict [Key] = Value
21 Return Platdict
22
23 # Obtain the map of each part.
24 Def Getsectionmap (strtmp, lable1 = sectionlable ):
25 TMP = strtmp. Split (lable1 [0])
26 TMP = [ELEM For ELEM In TMP If Len (ELEM)> 1]
27 TMP = [ELEM For ELEM In TMP If ELEM. rfind (lable1 [1])> 0]
28 Sectiondict = {}
29 For ELEM In TMP:
30 Key = ELEM [0: ELEM. Find (lable1 [1]):]
31 Value = ELEM [ELEM. Find (endlinelable) + Len (endlinelable):]
32 Sectiondict [Key] = Value
33 Return Sectiondict
34
35 # Get specific configuration values
36 Def Getvaluemap (strtmp ):
37 TMP = strtmp. Split (endlinelable)
38 TMP = [ELEM For ELEM In TMP If Len (ELEM)> 1]
39 Valuedict = {}
40 For ELEM In TMP:
41 If ELEM. Find (notelable)> 0:# If there is a comment, remove the comment.
42 ELEM = ELEM [0: ELEM. Find (notelable):]
43 ELEM = '' . Join (ELEM. Split ()) # Remove blank characters
44 Key = ELEM [0: ELEM. Find (equallable):]
45 Value = ELEM [ELEM. Find (equallable) + Len (equallable):]
46 Valuedict [Key] = Value
47 Return Valuedict
48
49 F = open (raw_input ( " Input File Name: " ), " RB " )
50 Strfilecontent = f. Read ()
51 F. Close ()
52 Vardict = {}
53
54 Var1 = getplatformmap (strfilecontent)
55
56 For K, V In Var1.items ():
57 Var2 = getsectionmap (V)
58 Dict3 = {}
59 For K2, V2 In Var2.items ():
60 Var3 = getvaluemap (V2)
61 Dict3 [k2] = var3
62 Vardict [k] = dict3
63
64 Print Vardict [ " Part2 " ] [ " Global " ] [ " IP " ]
Here, we only provide one idea and verify its feasibility. The specific implementation can be adapted in other languages such as C ++ to meet the requirements.