如何从文本文件中提取子集并将其存储在单独的文件中?

How to extract a subset from a text file and store it in a separate file?

我目前正在尝试使用 Python 从文本文件中提取信息。我想从文件中提取一个子集并将其存储在一个单独的文件中,而不是它在文本文件中出现的任何地方。为了让您了解我的文件是什么样子,这里有一个示例:

C","datatype":"double","value":25.71,"measurement":"Temperature","timestamp":1573039331258250},
{"unit":"%RH","datatype":"double","value":66.09,"measurement":"Humidity","timestamp":1573039331258250}]

在这里,我想提取"value"和它旁边的相应数字。我尝试了各种技术,但都没有成功。我试图遍历文件并停在我有 "value" 的地方,但这没有用。

这是代码示例:

with open("DOTemp.txt") as openfile:
    for line in openfile:
        for part in line.split():
            if "value" in part:
                print(part)

首先使用,(逗号)作为分隔符进行拆分,然后使用:作为分隔符拆分相应的字符串。 如果需要 trim 前导和尾随 "" 然后与值

进行比较

return 由 "value" 键标记的值的简单解决方案:

with open("DOTemp.txt") as openfile:
    for line in openfile:
        line = line.replace('"', '')
        for part in line.split(','):
            if "value" in part:
                print(part.split(':')[1])

请注意,默认情况下 str.split() 在空格处拆分。在最后一行中,如果我们打印列表的元素零,它将只是 "value"。如果您希望将其用作 int 或 float,只需将其转换为 return 即可。

以下代码适合您:

file1 = open("untitled.txt","r")
data = file1.readlines()

#Convert to a single string
val = ""
for d in data:
    val = val + d

#split string at comma
comma_splitted = val.split(',')

#find the required float
for element in comma_splitted:
    if 'value' in element:
        out = element.split('"value":')[1]
        print(float(out))

我假设您的输入文件是 json 字符串(字典列表)(查看文件示例)。如果是这样的话,也许你可以试试这个。

import json

#Assuming each record is a dictionary
with open("DOTemp.txt") as openfile:
    lines = openfile.readlines()
    records = json.loads(lines)
    out_lines = list(map(lambda d: d.get('value'), records))
    with open('DOTemp_out.txt', 'w') as outfile:
        outfile.write("\n".join(out_lines))