How to use different Python slicing string delimiter

How to use different split delimited string

How to use different segmentation delimiter string, a string based on the resolution delimiter, the character comprising a plurality of different delimiter character as

s = '12 ;; 7.osjd; .jshdjdknx + 'wherein;. + is the separator

What are the solutions?

Method 1: str.split () method, a delimiter per treatment

# / Usr / bin / to python3! 
DEF go_split (S, Symbol): 
    Result = [S]
     for I in Symbol: 
        Median = []
         # conventional method 
        # for X in Result: 
        #      median.extend (x.split (I) 
        # list parsing 
        # [median.extend (y.split (I)) for Y in Y IF Result] 
        # Map higher-order functions, map generation iterables 
        for Z in Map ( the lambda X: x.split (I), Result ): 
            median.extend (Z)       
        # above three methods can solve the problem 
        Result = Median   
    # Remove empty string 
    return [X for X in Result IF X]
 IF  the __name__ == " __main__ " :
     # define the initial string 
    S = ' 12 is ;; 7.osjd; .jshdjdknx + ' 
    # define separator 
    Symbol = ' ;. / + ' 
    Result = go_split (S, Symbol)
     Print (Result)

 

Method 2: re.split () method, a one-time resolved all strings, recommended

# ! / Usr / bin / to python3 
Import Re 

DEF go_split (S, Symbol):
     # spliced regex 
    Symbol = " [ " + Symbol + " ] + " 
    # split disposable string 
    Result = re.split (Symbol, S )
     # remove null character 
    return [X for X in Result IF X] 

IF  the __name__ == " __main__ " :
     # define the initial string 
    S = ' 12 is ;; 7.osjd; .jshdjdknx + ' 
    # define separator
    symbol = ';./+'
    result = go_split(s, symbol)
    print(result)

 

A small text-to-speech program, the text after the split, saying a word read out:

# - * - Coding: UTF-. 8 - * - 
Import pyttsx3
 Import Re
 Import OS 
Engine = pyttsx3.init ()
 # split description 
DEF go_split (S, Symbol):
     # spliced regex 
    Symbol = " [ " + + Symbol " ] + " 
    # disposable string divided 
    Result = re.split (Symbol, S)
     # remove null character 
    return [X for X in Result IF X]
 # Analyzing string contains only the Chinese: 
DEF reflag (List):
     forI in Range (len (List)):
         for CH in List [I]:
             IF CH> = ' \ u4e00 '  and CH <= ' \ u9fa5 ' :
                 return I
 DEF Reader (List, In Flag):
     IF In Flag == None :
         Print ( " This article does Chinese translation " )
     the else :
         Print ( " This article has Chinese translation " ) 
    lenght = len (List) 
    i = 0
    the while I < lenght: 
        InStr = INPUT ( " input operation: " + " \ n- " 
                      " . 1: Reading continue to listen to the phrase " + " \ n- " 
                      " 2: continue to listen to the read word " + " \ n- " + " : " )
         IF InStr == " . 1 " : 
            engine.say (List [I]) 
            Print (List [I]) 
            engine.runAndWait () 
        IF InStr == " 2 " :
            i = i + 1
            print(list[i])
            engine.say(list[i])
            engine.runAndWait()
    print(list)
def start():
    dirlsit= os.listdir('reader')
    for i in range(len(dirlsit)):
        print(str(i)+":"+str(dirlsit[i]))
    choice = int(input("输入选择的文章序号:"))
    with open("reader/"+dirlsit[choice],'r',encoding='utf-8') as f:
        article = f.read()
        article = article.replace('\n', '')
        # 定义分隔符
        symbol = ',;.:'
        list = go_split(article,symbol)
        if filsh(list):
            flag = reflag(list) - 1
        else:flag=None
        rate = engine.getProperty('rate')
        engine.setProperty('rate', rate-25)
        reader(list,flag)

start()

Article format:

Night after night, she came to tuck me in, even long after my childhood years. Following her longstanding custom, she'd lean down and push my long hair out of the way, then kiss my forehead.
I don't remember when it first started annoying me — her hands pushing my hair that way. But it did annoy me, for they felt work-worn and rough against my young skin. Finally, one night, I shouted out at her, "Don't do that anymore —your hands are too rough!" She didn't say anything in reply. But never again did my mother close out my day with that familiar expression of her love.
Time after time, with the passing years, my thoughts returned to that night. By then I missed my mother's hands, missed her goodnight kiss on my forehead. Sometimes the incident seemed very close, sometimes far away. But always it lurked, in the back of my mind.
Well, the years have passed, and I'm not a little girl anymore. Mom is in her mid-seventies, and those hands I once thought to be so rough are still doing things for me and my family. She's been our doctor, reaching into a medicine cabinet for the remedy to calm a young girl's stomach or soothe the boy's scraped knee. She cooks the best fried chicken in the world... gets stains out of blue jeans like I never could...
Now, my own children are grown and gone. Mom no longer has Dad, and on special occasions, I find myself drawn next door to spend the night with her. So it was late on Thanksgiving Eve, as I slept in the bedroom of my youth, a familiar hand hesitantly run across my face to brush the hair from my forehead. Then a kiss, ever so gently, touched my brow.
In my memory, for the thousandth time , I recalled the night my young voice complained, "Do not do that anymore -! Your hands are too rough" Catching Mom's hand in hand, I blurted out how sorry I was for that night. by Thought SHE'd Remember the I, AS DID But the I DID not know the What Mom at Talking the About the I WAS HAD She Forgotten -.. and Forgiven -. Long ago Member 
That night, the I Fell asleep with A new new Appreciation for My Mother and HER Gentle Caring . hands and the guilt that I had carried around for so long was nowhere to be found. 
[Chinese Lesson: 
following her I fall asleep, as I tuck, then leaned over and gently poke in my review Long face, kissed my forehead. Day after day, the mother has maintained this habit, even though I am no longer a child, but it all remains unchanged. 
I do not know when to begin, the mother of this habit gradually makes me feel unhappy ---- I do not like her pair of calloused hands so across my delicate skin. Finally, one night, I shouted out at her again: "Do not do that anymore - your hands are too rough!" Mother speechless. But never again did not use this way to express my love to my familiar end to the day.
As the days passed, as time goes by, I always could not help but think of that night. I missed my mother's hands, missed her kiss on my forehead, "Good night." This desire suddenly away suddenly near, but always lurking somewhere in the depths of my soul. 
Several years later, I matured, not a little girl anymore. Mother has come to seventy years of age, she has never stopped worked hard, and with her I had been regarded as "rough" hand still doing things for me and my family. She is our family doctor, when the little girl stomach, she would find out from the stomach medicine to the medicine cabinet, when the little boy scraped knee, she will go to appease his pain. She cooks the best fried chicken in the world, gets stains out of blue jeans was no trace ...... 
Now, my own children are grown and have their own life, mother but not the father's companionship. On one occasion, happens to be the night before Thanksgiving, I decided to sleep in the bedroom next to the mother, the accompany her through the night. This is my childhood bedroom, everything is so familiar, there is a familiar hand hesitantly passing from my face, combed my hair and forehead, then a kiss, as in the past with tenderness and landed softly on my forehead. 
In my memory, for the thousandth time I recalled the night and my young voice complained: "Do not do this, your hands are too rough!" I grabbed her hand, I have to say that fall one night deep guilt. I think she must like me, to do that night vividly. However, my mother did not know what to say. She had forgotten ----- already forgiven me. 
That night, I took my gratitude for the new mother fell asleep, I appreciate her gentle, and her caring hands. Over the years the pressure of guilt in my heart also will disappear.

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12594409.html