88519


置換したい文章を`body0x_before="ooooo"`の形で変数に入れて、文章の中で`replaceWord =
`の中にヒットする文字列があれば、その文字列をreplaceするという処理を行いたいです。

置換したい文章は数千個あり、例では文章の長さは短いですが、実際に`body0x_before="ooooo"`に入れる文章は１つにつき1000文字以上あります。

例)  
`body04_before="i like mcdonalds"`は
`["mcdonalds","chicken"]`がヒットするので、アウトプットが下記のように出るのを期待しています。

[code]

      before             after
        ・　　　　　　　　　　　・
        ・　　　　　　　　　　　・
        ・　　　　　　　　　　　・
    5 i like mcdonalds　　i like chicken
    
[/code]

根本的に何か間違っているように思えるのですが、どのように調べたら良いか分からずご教授いただけると幸いです。

スクリプト

[code]

    import os.path, time, re
    import pandas as pd
    import csv
    
    
    body01_before="test1234"
    body02_before="test9012"
    body03_before="test5678"
    body04_before="i like mcdonalds"
    body05_before="I was born april"
    
    replaceWord = [
                    ["test9012","te1210st"],
                    ["test5678","8579"],
                    ["test1234","1349"],
                    ["april","August"],
                    ["mcdonalds","chicken"],
    
    ]
    
    cols = ['before','after']
    df = pd.DataFrame(index=[], columns=cols)
    
    for word in replaceWord:
        
        body01_after = re.sub(word[0], word[1], body01_before)
        body02_after = re.sub(word[0], word[1], body02_before)
        body03_after = re.sub(word[0], word[1], body03_before)
        body04_after = re.sub(word[0], word[1], body04_before)
        body05_after = re.sub(word[0], word[1], body05_before)
    
        df=df.append({'before':body01_before,'after':body01_after}, ignore_index=True)
        
    #df.head()
    print(df)
    
    df.to_csv('test_replace.csv')
    
[/code]

現状の結果

[code]

         before     after
    0  test1234  test1234
    1  test1234  test1234
    2  test1234      1349
    3  test1234  test1234
    4  test1234  test1234
    
[/code]

期待する結果

[code]

      before    after
    1 test1234  1349
    2 test9012  te1210st
    3 test5678  8579
    4 april     I was born August
    5 mcdonalds i like checkin
    
[/code]


CC BY-SA 4.0
2022-04-26T06:52:25.103
88511
2022-04-27T12:44:18.317
2022-04-26T09:00:52.830
3060
52109
question
python python3
文章内の文字列をreplaceを使用して置換したい

[code]

    from functools import reduce
    import pandas as pd
    
    replaceWord = [
        [ 'test9012', 'te1210st'],
        [ 'test5678',     '8579'],
        [ 'test1234',     '1349'],
        [    'april',   'August'],
        ['mcdonalds',  'chicken'],
    ]
    
    before = [
        'test1234',
        'test9012',
        'test5678',
        'i like mcdonalds',
        'I was born april',
    ]
    after = [reduce(lambda i, j: i.replace(*j), replaceWord, text) for text in before]
    df = pd.DataFrame({'before': before, 'after': after})
    
    print(df)
    
    #
                 before              after
    0          test1234               1349
    1          test9012           te1210st
    2          test5678               8579
    3  i like mcdonalds     i like chicken
    4  I was born april  I was born August
    
[/code]


CC BY-SA 4.0
2022-04-26T12:31:20.310
88519
2022-04-27T12:44:18.317
2022-04-27T12:44:18.317
47127
47127
88511
answer