简体   繁体   中英

Dictionary from a String with particular structure

I am using python 3 to read this file and convert it to a dictionary.

I have this string from a file and I would like to know how could be possible to create a dictionary from it.

[User]
Date=10/26/2003
Time=09:01:01 AM
User=teodor
UserText=Max Cor
UserTextUnicode=392039n9dj90j32

[System]
Type=Absolute
Dnumber=QS236
Software=1.1.1.2
BuildNr=0923875
Source=LAM
Column=OWKD

[Build]
StageX=12345
Spotter=2
ApertureX=0.0098743
ApertureY=0.2431899
ShiftXYZ=-4.234809e-002

[Text]
Text=Here is the Text files
DataBaseNumber=The database number is 918723

..... (There are more than 1000 lines per file)...

On the text I have "Name=Something" and then I would like to convert it as follows:

{'Date':'10/26/2003',
'Time':'09:01:01 AM'
'User':'teodor'
'UserText':'Max Cor'
'UserTextUnicode':'392039n9dj90j32'.......}

The word between [ ] can be removed, like [User], [System], [Build], [Text], etc...

In some fields there is only the first part of the string:

[Colors]
Red=
Blue=
Yellow=
DarkBlue=

What you have is an ordinary properties file . You can use this example to read the values into map:

try (InputStream input = new FileInputStream("your_file_path")) {
    Properties prop = new Properties();
    prop.load(input);

    // prop.getProperty("User") == "teodor"

} catch (IOException ex) {
  ex.printStackTrace();
}

EDIT:
For Python solution, refer to the answerred question .
You can use configparser to read .ini , or .properties files (format you have).

import configparser

config = configparser.ConfigParser()
config.read('your_file_path')

# config['User'] == {'Date': '10/26/2003', 'Time': '09:01:01 AM'...}
# config['User']['User'] == 'teodor'
# config['System'] == {'Type': 'Abosulte', ...}

I would suggest to do some cleaning to get rid of the [] lines.

After that you can split those lines by the "=" separator and then convert it to a dictionary.

Can easily be done in python. Assuming your file is named test.txt . This will also work for lines with nothing after the = as well as lines with multiple = .

d = {}
with open('test.txt', 'r') as f:
    for line in f:
        line = line.strip() # Remove any space or newline characters
        parts = line.split('=') # Split around the `=`
        if len(parts) > 1:
            d[parts[0]] = ''.join(parts[1:])
print(d)

Output:

{
  "Date": "10/26/2003",
  "Time": "09:01:01 AM",
  "User": "teodor",
  "UserText": "Max Cor",
  "UserTextUnicode": "392039n9dj90j32",
  "Type": "Absolute",
  "Dnumber": "QS236",
  "Software": "1.1.1.2",
  "BuildNr": "0923875",
  "Source": "LAM",
  "Column": "OWKD",
  "StageX": "12345",
  "Spotter": "2",
  "ApertureX": "0.0098743",
  "ApertureY": "0.2431899",
  "ShiftXYZ": "-4.234809e-002",
  "Text": "Here is the Text files",
  "DataBaseNumber": "The database number is 918723"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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