Python:将文本文件的行转换并连接到具有列表值的字典中

Python:将文本文件的行转换并连接到具有列表值的字典中,python,file,dictionary,Python,File,Dictionary,我有一个文本文件,其中包含四行任意(非Python)块列表,如下所示: WHAT EVER 0.00000904 17577 FOO BAR 7.00000031 426 该文件包含数千个这样的块。如何将文件中的数据转换为列表字典,其中键是每个块的前两行,连接在一起,接下来的两行是列表值?例如: {'WHATEVER': [0.00000904, 17577], 'FOOBAR': [7.00000031, 426]} 您可以执行以下操作: import os # set base pa

我有一个文本文件,其中包含四行任意(非Python)块列表,如下所示:

WHAT
EVER
0.00000904
17577

FOO
BAR
7.00000031
426
该文件包含数千个这样的块。如何将文件中的数据转换为列表字典,其中键是每个块的前两行,连接在一起,接下来的两行是列表值?例如:

{'WHATEVER': [0.00000904, 17577], 'FOOBAR': [7.00000031, 426]}

您可以执行以下操作:

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]}
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)
请尝试以下操作:

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]}
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
):


我希望这有帮助。

谢谢!这适用于odd_lines.txt。然而,我在更大的文本文件中遇到了问题,每一行的格式都是自己的字典;它看起来像:{u'WHAT':[]}{u'EVER':[]}{u'0.00100001':[]}{u'10101':[]}{u'FOO':[]}{u'BAR':[]}{u'0.00500761':[]}{u'7991282':[]}{u':[]}请共享一个较大的文本文件,您认为可能会出现这种不想要的结果。@Skeletonsaurs,我在这里束手无策,因为您展示的起点是使用我在这里提供的代码。除非您将起始文件更改为输出错误解决方案的文件,否则我真的做不了太多。这解决了问题(因为有些是从数字开始的):将正则表达式从data=[re.split(“\n(?=[0-9])”,x)for x in data]更改为data=[re.split(“\n(?=[0-9]+$”,x)for x in data]这在Python3上不起作用,因为map不再返回列表。请改用listcomp。当我运行您的解决方案时,我遇到了这个问题:文件“test.py”,第11行,在nums=map(float,nums)ValueError中:无法将字符串转换为float: