我有一个文本文件,其中包含四行的任意(非Python)列表,如下所示:
WHAT
EVER
0.00000904
17577
FOO
BAR
7.00000031
426
该文件包含数千个这样的块。如何将文件中的数据转换为列表字典,其中键是每个块的前两行,连接,接下来的两行是列表值?例如:
{'WHATEVER': [0.00000904, 17577], 'FOOBAR': [7.00000031, 426]}
答案 0 :(得分:1)
尝试以下方法:
import re
# Open the file
data = open('odd_lines.txt').read()
# Split on the double newline characters
data = data.split("\n\n")
# Split each element of the data list on the newline characters followed by a float
data = [re.split("\n(\d+\.\d+)", x) for x in data]
# Put the data in a dictionary with the key being the first element of each element of the data list.
# Make sure to replace the newline character with an empty space
output = {x[0].replace("\n",""):[float(y) for y in x[1:]] for x in data}
print(output)
这应该产生:
#{'FOOBAR': [7.00000031, 426], 'WHATEVER': [0.00000904, 17577]}
以下是起始文件(odd_lines.txt
):
WHAT
EVER
0.00000904
17577
FOO
BAR
7.00000031
426
我希望这会有所帮助。
答案 1 :(得分:0)
您可以执行以下操作:
import os
# set base path to main dir of target file
root = os.getcwd()
# split on double spaces
vals = open(os.path.join(root, 'test.txt'), 'r').read().split('\n\n')
# create empty dictionary to store values
valdict = {}
# iterate over each item which should contain the keys and values
for val in vals:
# fill in dict with key and turn numbers into float and dict value as float list
key = ''.join(val.split('\n')[0:2])
nums = val.split('\n')[2:]
nums = map(float, nums)
valdict[key] = nums
valdict
# output: {'FOOBAR': [7.00000031, 426.0], 'WHATEVER': [9.04e-06, 17577.0]}