简体   繁体   English

python:在字符串末尾移动一个特定的单词

[英]python:Move a specific word at the end of a string

i learn python and i do a discord bot.我学习了 python,我做了一个 discord 机器人。 I have some difficulties to print the element after "anivia".在“anivia”之后打印元素有一些困难。 i cant say if there is "anivia" in the 'texte' and i can count him but i don't know how to print the element after "anivia", if someone can help me please:)我不能说'texte'中是否有“anivia”,我可以数他,但我不知道如何在“anivia”之后打印元素,如果有人可以帮助我:)

@bot.command()
async def counter(ctx, *champion):
    champion = " ".join(champion)
    url = "https://u.gg/lol/champions/"
    counter = "/counter"
    uurl = url + champion + counter
    await ctx.send(uurl)

    import urllib.request
    with urllib.request.urlopen(uurl) as response:
        texte = response.read()
    if ("anivia" in str(texte)):
        print("Le mot existe !")
    else:
        print("Le mot n'existe pas!")
    test = str(texte)

    z = test.count('anivia')
    print(z)

I can count 9 "anivia" with z and i want to print the next element after all the anivia (example: " hello im anivia and i like anivia test": and , test ).我可以用 z 数 9 个“anivia”,并且我想在所有 anivia 之后打印下一个元素(例如:“hello im anivia and i like anivia test”: and , test )。

Thanks for your help:)谢谢你的帮助:)

If you're familiar with regular expressions (regex), this becomes very simple:如果您熟悉正则表达式 (regex),这将变得非常简单:

import re

# This pattern will capture the first word that comes after "anivia"
pattern = r'anivia (\w+)'

# Using this string as our example input
example_string = "anivia first anivia second and finally anivia third"

results = re.findall(pattern, example_string)

print(results)  # Output: ['first', 'second', 'third']

Here is an approach that uses an auxiliary variable to mark when the next word needs to be printed.这是一种使用辅助变量来标记何时需要打印下一个单词的方法。

test_string = "Hello, I am anivia on mid or anivia jungle"

do_print = False
splitted = test_string.split()
for word in splitted:
    if do_print:
        do_print = False
        print(word)
    if word == "anivia":
        do_print = True

Output: Output:

on
jungle

yeah, the those solution works with strings (i tried too with regex) but是的,这些解决方案适用于字符串(我也尝试过使用正则表达式)但是


    do_print = False
    splitted = test_string.split()
    for word in splitted:
        # print(word)

        if do_print:
            do_print = False
        if word == "anivia":
            do_print = True
    
    test_string = str(texte)

    do_print = False
    splitted = test_string.split()
    for word in splitted:
        # print(word)

        if do_print:
            do_print = False
            # print(word)
        if word == "champion_id":
            do_print = True`` 

on the first case i have the ("on" and the "jungle") but with my str(texte), that's doesn't fonction :S.
 If someone knows why, the 2 test_strings are "strings" 

 ^^ ty for your answers :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python将单词移动到行尾 - move a word to the end of the line using python 程序从1个文件中读取内容并将从特定单词开始并以特定单词结尾的字符串写入python中的另一个文件 - program to Read content from 1 file and write string which is started from specific word and end with specific word into another file in python Python正则表达式特定单词,末尾带有单引号 - Python regex specific word with singe quote at end Python 在 google 中搜索以特定单词结尾的网站 - Python search website in google that end with specific word 比较特定的字符串和单词python - compare specific string to a word python 在字符串中查找特定单词 + 任意单词 + 特定单词 python - Find in string a specific word + whatever word + specific word python Python正则表达式匹配开始和结束字符串,并且必须包含特定的单词 - Python regular expression match start and end string and must contain specific word Python:获取 DataFrame 字符串 Object 中以特定单词开头和结尾的数字 - Python: Get numeric in a DataFrame String Object which start and end with specific word 如何在特定列中的每个字符串的末尾添加一个单词(熊猫数据框) - How to add a word to the end of each string in a specific column (pandas dataframe) Python:在末尾基于元素在列表中移动特定列表 - Python: Move specific lists within list, based on an element, at the end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM