分割字符串

Dividing a string

我有一个列表,我想拆分“And”中的每个字符串并将其加入第一个操作。

OLD_Txt=
["User enters validusername "MAXI" And password "768"",
"User enters phonenumber "76567898" And ZIPcode "97656"",
"User Verifys Country  "ENGLAND" And City "LONDON""]

我希望我的列表看起来像这样

New_Txt:

["User enters validusername "MAXI"", 
"User enters password "768"",
"User enters phonenumber "76567898"",
"User enters ZIPcode "97656"",
"User Verifys Country "ENGLAND"",
"User Verifys City "LONDON""]

如果你理解正确,你想要 join 你的字符串。 所以你真的想:

New_Txt = ' '.join(OLD_Txt)
OLD_Txt= [
    'User enters validusername "MAXI" And password "768"', 
    'User enters phonenumber "76567898" And ZIPcode "97656"', 
    'User Verifys Country "ENGLAND" And City "LONDON"']

base_word = 'User '

new_text = []
for string_obj in OLD_Txt:
    first_part, second_part = string_obj.split('And')
    verb_for_second_part = first_part.split(' ')[1] 
    # from split you ll get ["User" "enters", "validusername", "MAXI"]
    # and you need text at index 1 i.e: verb_at_index
    new_text.append(first_part)

    second_part = base_word + verb_for_second_part + second_part
    new_text.append(second_part)

print(new_text)

输出

['User enters validusername "MAXI" ', 'User enters password "768"', 'User enters phonenumber "76567898" ', 'User enters ZIPcode "97656"', 'User Verifys Country "ENGLAND" ', 'User Verifys City "LONDON"']