如何在python中从巨大的字符串中提取子字符串?

2022-09-28 21:12:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有巨大的字符串(7-10k字符)从日志文件,我需要自动提取和制表信息。每个字符串包含大约40个由不同人员输入的值。举例说明

Example string 1.) 'Color=Blue, [randomJunkdataExampleHere] Weight=345Kg, Age=34 Years, error#1 randomJunkdataExampleThere error#1'
Example string 2.) '[randomJunkdataExampleHere]   Color=Red 42, Weight=256 Lbs., Age=34yers, error#1, error#2'
Example string 3.) 'Color=Yellow 13,Weight=345lbs., Age=56  [randomJunkdataExampleHere]'

所期望的结果是一个新的字符串,甚至是一个字典,用于组织数据和准备数据库条目(每行数据对应一个字符串)

Color,Weight,Age,Error#1Count,Error#2Count
blue,345,34,2,0
red,256,24,1,1
yellow,345,56,0,0

考虑使用检索对于每个列/值,但是由于用户输入数据的方式存在差异,我不知道如何捕获我想要提取的数字。也不知道如何捕捉字符串中出现“Error#1Count”的次数。你知道吗

import re
line = '[randomJunkdataExampleHere] Color=Blue, Weight=345Kg, Age=34 Years, error#1, randomJunkdataExampleThere error#1'

try:    
    Weight = re.search('Weight=(.+?), Age',line).group(1)
except AttributeError:
    Weight = 'ERROR'

目标/结果:

Color,Weight,Age,Error#1Count,Error#2Count
blue,345,34,2,0
red,256,24,1,1
yellow,345,56,0,0

Tags: 数据字符串agestringexampleerrorblueredcolorweightyearsrandomjunkdataexamplethererandomjunkdataexamplehere
1条回答
网友
1楼 ·

如上所述,10000个字符真的不是什么大问题。你知道吗

import time

example_string_1 = 'Color=Blue, Weight=345Kg, Age=34 Years, error#1, error#1'
example_string_2 = 'Color=Red 42, Weight=256 Lbs., Age=34 yers, error#1, error#2'
example_string_3 = 'Color=Yellow 13, Weight=345lbs., Age=56'

def run():
examples = [example_string_1, example_string_2, example_string_3]
dict_list = []

for example in examples:
    # first, I would suggest tokenizing the string to identify individual data entries.
    tokens = example.split(', ')
    my_dict = {}
    for token in tokens: # Non-error case
        if '=' in token:
            subtokens = token.split('=') # this will split the token into two parts, i.e ['Color', 'Blue']
            my_dict[subtokens[0]] = subtokens[1]
        elif '#' in token: # error case. Not sure if this is actually the format. If not, you'll have to find something to key off of.
            if 'error' not in my_dict or my_dict['error'] is None:
                my_dict['error'] = [token]
            else:
                my_dict['error'].append(token)
    dict_list.append(my_dict)

# Now lets test out how fast it is.
before = time.time()

for i in range(100000): # run it a hundred thousand times
    run()

after = time.time()

print("Time: {0}".format(after - before))

收益率:

Time: 0.5782015323638916

看到了吗?还不错。现在只剩下遍历字典并记录所需的度量。你知道吗

热门问题