[英]Dictionary from a String with particular structure
I am using python 3 to read this file and convert it to a dictionary.我正在使用 python 3 读取此文件并将其转换为字典。
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)... .....(每个文件有1000多行)...
On the text I have "Name=Something"
and then I would like to convert it as follows:在文本上我有
"Name=Something"
,然后我想将其转换如下:
{'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...
[ ]
之间的单词可以去掉,如[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:
您可以使用此示例将值读入 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 .对于 Python 解决方案,请参阅已回答的问题。
You can use configparser to read .ini
, or .properties
files (format you have).您可以使用configparser读取
.ini
或.properties
文件(您拥有的格式)。
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.可以在 python 中轻松完成。 Assuming your file is named
test.txt
.假设您的文件名为
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: 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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.