简体   繁体   English

从txt文件中提取数据

[英]Extract the data from txt file

I want to extract the value of a, b and c from text_file.txt using Python.我想使用 Python 从 text_file.txt 中提取 a、b 和 c 的值。

text_file.txt文本文件.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$

The following code will discover the variables (compatible with lists, dictionaries and sets as well) from the file and add them as local variables as requested:以下代码将从文件中发现变量(也与列表、字典和集合兼容),并根据要求将它们添加为局部变量:

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}')
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)

Prints:印刷:

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

You can make use of the regular expressions module for your particular issue.您可以针对您的特定问题使用正则表达式模块。 You can extract all the values into a list and then extract off of it for your application.您可以将所有值提取到一个列表中,然后从中提取出来用于您的应用程序。

import re

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

output:输出:

$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']
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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM