I am new to python. I have a txt file which contains input data for a 3D model:
我是python的新手。我有一个txt文件,其中包含3D模型的输入数据:
...
# comments
rfk = [1e-5, 1e-7, 1e-1]
rsig = [0.0005]
# comments
lengths = [[13,13,1.6],[13,13,1.6],[13,13,1.6]]
...
I guess the best way to extract the data for further processing is to save them into dictionaries - like this:
我想提取数据进行进一步处理的最佳方法是将它们保存到字典中 - 如下所示:
data1 = {rfk:[1e-5, 1e-7, 1e-1], rsig:[0.0005], lengths:[[13,13,1.6],[13,13,1.6],[13,13,1.6]]}
I now struggle a little bit with reading the file and saving the data (by ignoring the comments) like in my example as dictionaries. My approach for reading the data from the file was like this:
我现在正在努力阅读文件并保存数据(通过忽略注释),就像在我的例子中作为词典一样。我从文件中读取数据的方法是这样的:
for line in open("myfile.txt"):
li=line.strip()
if not li.startswith("#"):
# what to do here?
3 个解决方案
#1
0
We can use ast.literal_eval
to evaluate the right hand side into Python objects
我们可以使用ast.literal_eval来评估Python对象的右侧
from ast import literal_eval
with open('myfile.txt') as file:
d = {}
for line in file:
line = line.strip()
if not line.startswith('#'):
key, value = map(str.strip, line.split('='))
d[key] = literal_eval(value)
For your example data, d
is then
对于您的示例数据,d是
{'rfk': [1e-05, 1e-07, 0.1], 'rsig': [0.0005], 'lengths': [[13, 13, 1.6], [13, 13, 1.6], [13, 13, 1.6]]}
#2
0
You can use the string method partition
* to seperate your data to key and value for your dictionary,
The string method strip
to clean up your key's string from leading and tailing spaces
And then the built-in function eval
to extract the pythonic object from the value's string, as such:
您可以使用字符串方法partition *将数据分隔为键和字典值,字符串方法条从前导和尾随空格中清除键的字符串然后使用内置函数eval从中提取pythonic对象value的字符串,如下:
with open('myfile.txt') as file:
d = {}
for line in file:
line = line.strip()
if not line.startswith('#'):
key, _ ,value = line.partition('=')
d[key.strip()] = eval(value)
*partition
splits your string in the first occurance of your delimiter (which is '=' in this case)
and returns a list
of [leading string, delimiter, following string]
* partition在你的分隔符的第一次出现时拆分你的字符串(在这种情况下是'=')并返回[leading string,delimiter,follow string]的列表
#3
0
You can treat the at as json and add a few safety checks:
您可以将at视为json并添加一些安全检查:
import json
data = {}
with open('thefile.txt') as fobj:
for raw_line in fobj:
line = raw_line.strip()
if line and not line.startswith('#'):
try:
key, values = line.split('=', 1)
except ValueError:
print('skipping invalid line:')
print(line)
try:
data[key.strip()] = json.loads(values)
except json.JSONDecodeError
print('skipping invalid line (no JSON):')
print(line)
for your example data
contains:
对于您的示例数据包含:
{'lengths': [[13, 13, 1.6], [13, 13, 1.6], [13, 13, 1.6]],
'rfk': [1e-05, 1e-07, 0.1],
'rsig': [0.0005]}
#1
0
We can use ast.literal_eval
to evaluate the right hand side into Python objects
我们可以使用ast.literal_eval来评估Python对象的右侧
from ast import literal_eval
with open('myfile.txt') as file:
d = {}
for line in file:
line = line.strip()
if not line.startswith('#'):
key, value = map(str.strip, line.split('='))
d[key] = literal_eval(value)
For your example data, d
is then
对于您的示例数据,d是
{'rfk': [1e-05, 1e-07, 0.1], 'rsig': [0.0005], 'lengths': [[13, 13, 1.6], [13, 13, 1.6], [13, 13, 1.6]]}
#2
0
You can use the string method partition
* to seperate your data to key and value for your dictionary,
The string method strip
to clean up your key's string from leading and tailing spaces
And then the built-in function eval
to extract the pythonic object from the value's string, as such:
您可以使用字符串方法partition *将数据分隔为键和字典值,字符串方法条从前导和尾随空格中清除键的字符串然后使用内置函数eval从中提取pythonic对象value的字符串,如下:
with open('myfile.txt') as file:
d = {}
for line in file:
line = line.strip()
if not line.startswith('#'):
key, _ ,value = line.partition('=')
d[key.strip()] = eval(value)
*partition
splits your string in the first occurance of your delimiter (which is '=' in this case)
and returns a list
of [leading string, delimiter, following string]
* partition在你的分隔符的第一次出现时拆分你的字符串(在这种情况下是'=')并返回[leading string,delimiter,follow string]的列表
#3
0
You can treat the at as json and add a few safety checks:
您可以将at视为json并添加一些安全检查:
import json
data = {}
with open('thefile.txt') as fobj:
for raw_line in fobj:
line = raw_line.strip()
if line and not line.startswith('#'):
try:
key, values = line.split('=', 1)
except ValueError:
print('skipping invalid line:')
print(line)
try:
data[key.strip()] = json.loads(values)
except json.JSONDecodeError
print('skipping invalid line (no JSON):')
print(line)
for your example data
contains:
对于您的示例数据包含:
{'lengths': [[13, 13, 1.6], [13, 13, 1.6], [13, 13, 1.6]],
'rfk': [1e-05, 1e-07, 0.1],
'rsig': [0.0005]}