从txt文件中提取数据

时间:2020-09-07 11:56:36

标签: python python-3.x re

我想使用Python从text_file.txt中提取a,b和c的值。

text_file.txt

$This Script is written for value extraction$ 
a = 2.88 
b = 3.9 
c = 4.9 $this is a value for C$
d = 3.2 $not require for the program$

4 个答案:

答案 0 :(得分:1)

以下代码将从文件中发现变量(也与列表,字典和集合兼容),并根据要求将它们添加为局部变量:

import ast

vars = {}
# Open file for reading
with open('file.txt', 'r') as f:
    lines = f.readlines()
    for l in lines:
        try:
            # Find comment begining
            end = l.index('$')
        except ValueError:
            # If no comment, set end to last char
            end = len(l)
        # Set the part to parse
        assignment = l[:end]
        # Try split assignment to the variable name and value
        x = assignment.split('=')
        # If it's assignment (splitted to two parts)
        if 2 == len(x):
            var, value = x
            # Safe evaluate and add to dictionary
            vars[var.strip()] = ast.literal_eval(value.strip())

# {'a': 2.88, 'b': 3.9, 'c': 4.9, 'd': 3.2}
print(vars)

# Set as local variables
for k,v in vars.items():
    locals()[k] = v

# a: 2.88
print(f'a: {a}')
# b: 3.9
print(f'b: {b}')
# c: 4.9
print(f'c: {c}')
# d: 3.2
print(f'd: {d}')

答案 1 :(得分:1)

with open('text_file.txt', 'r') as f_in:
    d = dict(re.findall(r'^\s*([a-z]+)\s*=\s*([^$\s]+)', f_in.read(), flags=re.M))

print(d)

打印:

{'a': '2.88', 'b': '3.9', 'c': '4.9', 'd': '3.2'}

答案 2 :(得分:1)

对于特定问题,您可以使用正则表达式模块。您可以将所有值提取到列表中,然后从中提取出来以用于您的应用程序。

import re

f = open('text_file.txt')
text = f.read()
print(text)
match = re.findall(r'\w\s=\s\d+.\d+', text)
print(match)

输出:

$This Script is written for value extraction$
a = 2.88
b = 3.9
c = 4.9 $this is a value for C$
d = 3.2 $not require for the program$
['a = 2.88', 'b = 3.9', 'c = 4.9', 'd = 3.2']

答案 3 :(得分:0)

import re

with open("text_file.txt", "r") as fd:
    content = fd.read()

nr = re.findall("[\.0-9]+", content)
a = int(nr[0])
b = int(nr[1])
c = int(nr[2])