Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

Python: string.rfind() with any whitespace character?

I'm trying to split text into chunks to send to Google's text-to-speech engine (which accepts max. 5000 characters per query). I want to split longer files on a whitespace character with a maximum length of 5000 characters. My current code (using a chunk size of 15 instead of 5000):

def split_text(text) -> list:
    start = 0
    chunk_size = 15
    chunk = ''
    chunks = []
    chunks_remaining = True

    while chunks_remaining:
        end = start + chunk_size
        if end >= len(text):
            chunks_remaining = False
        chunk = text[start:end]
        end = chunk.rfind(' ') + start
        chunks.append(text[start:end] + "...")
        start = end+1
    return chunks


def main():
    text = "This is just a text string for demonstrative purposes."
    chunks = split_text(text)
    print(chunks)

Is there a way to replace chunk.rfind(' ') with something that accepts any whitespace character?

question from:https://stackoverflow.com/questions/65930803/python-string-rfind-with-any-whitespace-character

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    i = -1
    while (true):
        if chunk[i] in ['
','
','', ' ']:
            end = i
        else:
            i -= 1
        

Would something like that work for you? It would scan the chunk from the end for any whitespace character.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...