开发者

Grab a keyword and the text between keywords in Python

开发者 https://www.devze.com 2023-04-05 19:37 出处:网络
Firt thing I\'d like to say is this place has helped me more than I could ever repay.I\'d like to say thanks to all that have helped me in the past :).

Firt thing I'd like to say is this place has helped me more than I could ever repay. I'd like to say thanks to all that have helped me in the past :).

I am trying to devide up some text from a specific style message. It is formated like this:

DATA|1|TEXT1|STUFF: some random text|||||
DATA|2|TEXT1|THINGS: some random text and|||||
DATA|3|TEXT1|some more random text and stuff|||||
DATA|4|TEXT1|JUNK: crazy randomness|||||
DATA|5|TEXT1|CRAP: such random stuff I cant believe how random|||||

I have code shown below that combines the text adding a space between words and adds it to a string named "TEXT" so it looks like this:

STUFF: some random text THINGS: some random text and some more random text and stuff JUNK: crazy randomness CRAP: such random stuff I cant believe how random

I need it formated like this:

DATA|1|TEXT1|STUFF: |||||
DATA|2|TEXT1|some random text|||||
DATA|3|TEXT1|THINGS: |||||
DATA|4|TEXT1|some random text and|||||
DATA|5|TEXT1|some more random text and stuff|||||
DATA|6|TEXT1|JUNK: |||||
DATA|7|TEXT1|crazy randomness|||||
DATA|8|NEWTEXT|CRAP: |||||
DATA|9|NEWTEXT|such random stuff I cant believe how random|||||

The line numbers are easy, I have that done as well as the carraige returns. What I need is to grab "CRAP" and change the part that says "TEXT1" to "NEWTEXT".

My code scans the string looking for keywords then adds them to their own line then adds text below them followed by the next keyword on its own line etc. Here is my code I have so far:

#this combines all text to one line and adds to a string
while current_segment.move_next('DATA')
    TEXT = TEXT + " " + current_segment.field(4).value

KEYWORD_LIST  = [STUFF:', THINGS:', JUNK:']
KEYWORD_LIST1 = [CRAP:']

#this splits the words up to search through
TEXT_list = TEXT.split(' ')

#this searches for the first few keywords then stops at the unwanted one
for word in TEXT_list:
    if word in KEYWORD_LIST:
        my_output = my_output + word
    elif word in KEYWORD_LIST1:
        break
    else:
        my_output = my_output + ' ' + word

#this searches for the unwanted keywords leaving the output blank until it reaches the wanted keyword
for word1 in TEXT_list:
    if word1 in KEYWORD_LIST:
        my_output1 = ''
    elif word1 in KEYWORD_LIST1:
        my_output1 = my_output1 + word1 + '\n'
    else:
        my_output1 = my_output1 + ' ' + word1

#my_output is formatted back the way I want deviding up the text into 65 or less character lines

MAX_LENGTH = 65
my_wrapped_output  = wrap(my_output,MAX_LENGTH)
my_wrapped_output1 = wrap(my_output1,MAX_LENGTH)
my_output_list     = my_wrapped_output.split('\n')
my_output_list1    = my_wrapped_output1.split('\n')

for phrase in my_output_list:
     if phrase == "":
          SetID +=1
          output = output + "DATA|" + str(SetID) + "|TEXT| |||||"
     else:
          SetID +=1
          output = output + "DATA|" + str(SetID) + "|TEXT|" + phrase + "|||||"

for phrase2 in my_output_list1:
     if phrase2 == "":
          SetID +=1
          output = output + "DATA|" + str(SetID) + "|NEWTEXT| |||||"
     else:
          SetID +=1
          output = output + "DATA|" + str(SetID) + "|NEWTEXT|" + phrase + "|||||"

#this populates the fields I need
value = output

Then I format the "my_output" and "my_output1" adding the word "NEWTEXT" where it goes. This code runs through each line looking for the keyword then puts that keyword and a carraige return in. Once it gets the other "KEYWORD_LIST1" it stops and drops the rest of the text then starts the next loop. My开发者_开发百科 problem is the above code gives my this:

DATA|1|TEXT1|STUFF: |||||
DATA|2|TEXT1|some random text|||||
DATA|3|TEXT1|THINGS: |||||
DATA|4|TEXT1|some random text and|||||
DATA|5|TEXT1|some more random text and stuff|||||
DATA|6|TEXT1|JUNK: |||||
DATA|7|TEXT1|crazy randomness|||||
DATA|8|NEWTEXT|crazy randomness|||||
DATA|9|NEWTEXT|CRAP: |||||
DATA|10|NEWTEXT|such random stuff I cant believe how random|||||

It is grabbing the text from before "KEYWORD_LIST1" and adding it into the NEWTEXT section. I know there is a way to make groups from the keyword and text after it but I am unclear on how to impliment it. Any help would be much appreciated.

Thanks.

This is what I had to do to get it to work for me:

KEYWORD_LIST  = ['STUFF:', 'THINGS:', 'JUNK:']
KEYWORD_LIST1 = ['CRAP:']

def text_to_message(text):
    result=[]
    for word in text.split():
        if word in KEYWORD_LIST or word in KEYWORD_LIST1:
            if result:
            yield ' '.join(result)
            result=[]
            yield word
        else:
            result.append(word)
    if result:
        yield ' '.join(result)

def format_messages(messages):
    title='TEXT1'
    for message in messages:
        if message in KEYWORD_LIST:
            title='TEXT1'
        elif message in KEYWORD_LIST1:
            title='NEWTEXT'
    my_wrapped_output  = wrap(message,MAX_LENGTH)
    my_output_list     = my_wrapped_output.split('\n')
    for line in my_output_list:
        if line = '':
            yield title + '|'
        else:
            yield title + '|' + line

for line in format_messages(text_to_message(TEXT)):
    if line = '':
        SetID +=1
        output = "DATA|" + str(SetID) + "|"
    else:
        SetID +=1
        output = "DATA|" + str(SetID) + "|" + line

#this is needed instead of print(line)
value = output 


  1. General tip: Don't try to build up strings accretively like this:

    my_output = my_output + ' ' + word
    

    instead, make my_output a list, append word to the list, and then, at the very end, do a single join: my_output = ' '.join(my_output). (See text_to_message code below for an example.) Using join is the right way to build strings. Delaying the creation of the string is useful because processing lists of substrings is more pleasant than splitting and unsplitting strings, and having to add spaces and carriage returns here and there.

  2. Study generators. They are easy to understand, and can help you a lot when processing text like this.


import textwrap

KEYWORD_LIST  = ['STUFF:', 'THINGS:', 'JUNK:']
KEYWORD_LIST1 = ['CRAP:']

def text_to_message(text):
    result=[]
    for word in text.split():
        if word in KEYWORD_LIST or word in KEYWORD_LIST1:
            if result:
                yield ' '.join(result)
                result=[]
            yield word
        else:
            result.append(word)
    if result:
        yield ' '.join(result)

def format_messages(messages):
    title='TEXT1'
    num=1
    for message in messages:
        if message in KEYWORD_LIST:
            title='TEXT1'
        elif message in KEYWORD_LIST1:
            title='NEWTEXT'
        for line in textwrap.wrap(message,width=65):
            yield 'DATA|{n}|{t}|{l}'.format(n=num,t=title,l=line)
            num+=1

TEXT='''STUFF: some random text THINGS: some random text and some more random text and stuff JUNK: crazy randomness CRAP: such random stuff I cant believe how random'''

for line in format_messages(text_to_message(TEXT)):
    print(line)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号