检查字符串是否包含特定单词并更改它

问题描述 投票:0回答:2

我是一名磁学专家,我正在尝试使用 python 实现常规问题的自动化。现在我正在处理该文件:

with open('test3.txt') as file:
    my_list = file.readlines()  
    b = [line.strip() for line in my_list]

结果,我得到了一个包含许多元素(字符串)的列表:

['"url": "https://services.swpc.noaa.gov/images/animations/d-rap/north-pole/d-rap/`latest`.png",', '"httpVersion": "http/2.0",', '"headers": [', '{', '"name": ":authority",', '"value": "services.swpc.noaa.gov"', '},', '{', '"name": ":method",', '"value": "HEAD"', '},', '{', '"name": ":path",', '"value": "/images/animations/d-rap/north-pole/d-rap/`latest`.png"', '"url": "https://services.swpc.noaa.gov/images/animations/d-rap/global_f05/d-rap/`latest`.png",', '"httpVersion": "http/2.0",', '"headers": [', '{', '"name": ":authority",', '"value": "services.swpc.noaa.gov"', '},', '{', '"name": ":method",', '"value": "HEAD"', '},', '{', '"name": ":path",', '"value": "/images/animations/d-rap/global_f05/d-rap/`latest`.png"', '},', '{', '"name": ":scheme",', '"value": "https"', '},', '{', '"name": "accept",', '"value": "*/*"', '},', '{', '"name": "accept-encoding",', '"value": "gzip, deflate, br"', '', '']

我的任务是找到所有包含

latest
的行并更改这个特定的单词,如下所示:

'"url": "https://services.swpc.noaa.gov/images/animations/d-rap/north-pole/d-rap/`latest1`.png",'

下一个包含

latest
的字符串应如下所示:

'"value": "/images/animations/d-rap/north-pole/d-rap/`latest2`.png"', '

等等,本质上,我只需要每次在“最新”这个词上加1,但我为此伤透了两天)请帮忙

这是我的“代码”,但它不起作用:

with open('test3.txt') as file:
    my_list = file.readlines()  
    b = [line.strip() for line in my_list]
    latest_count = 1
    latest_count2 = 2 
for i in range(len(b)):
     if b[i] ==  '"url": "https://services.swpc.noaa.gov/images/animations/d-rap/north-pole/d-rap/latest.png",':
        b[i] =   f'"url": "https://services.swpc.noaa.gov/images/animations/d-rap/north-pole/d-rap/latest{latest_count}.png",'
        latest_count += 1
     elif b[i] == '"value": "/images/animations/d-rap/global_f05/d-rap/latest.png"':
          b[i] = f'"value": "/images/animations/d-rap/global_f05/d-rap/latest{latest_count2}.png"'
          latest_count2 = 2
print(b)

尤其是在数千行这样的原始文件中,它们除了包含

latest

之外都是不同的
python list readlines
2个回答
0
投票

IIUC,你可以这样做:

with open("your_file.txt", "r") as f_in:
    cnt = 1
    for line in f_in:
        if "latest" in line:
            line = line.replace("latest", f"latest{cnt}")
            cnt += 1
        print(line.strip())

打印:

"url": "https://services.swpc.noaa.gov/images/animations/d-rap/north-pole/d-rap/`latest1`.png",
"httpVersion": "http/2.0",
"headers": [
{
"name": ":authority",
"value": "services.swpc.noaa.gov"
},
{
"name": ":method",
"value": "HEAD"
},
{
"name": ":path",
"value": "/images/animations/d-rap/north-pole/d-rap/`latest2`.png"
"url": "https://services.swpc.noaa.gov/images/animations/d-rap/global_f05/d-rap/`latest3`.png",
"httpVersion": "http/2.0",
"headers": [
{
"name": ":authority",
"value": "services.swpc.noaa.gov"
},
{
"name": ":method",
"value": "HEAD"
},
{
"name": ":path",
"value": "/images/animations/d-rap/global_f05/d-rap/`latest4`.png"
},
{
"name": ":scheme",
"value": "https"
},
{
"name": "accept",
"value": "*/*"
},
{
"name": "accept-encoding",
"value": "gzip, deflate, br"


0
投票

也许下面的代码可以帮助你:

def update_latest(lst):
    count_latest = 0
    updated_list = []

    for item in lst:
        if "latest" in item:
            count_latest += 1
            updated_list.append("latest{}{}".format(count_latest, item[6:]))
        else:
            updated_list.append(item)

    return updated_list

updated_list = update_latest(b)
print(updated_list)

例如,给定输入列表

["latestsjfgjhgf", "wyrt", "latestfgjhgf"]
,上面的函数会产生
['latest1sjfgjhgf', 'wyrt', 'latest2fgjhgf']

希望这有帮助!愿代码与您同在...

© www.soinside.com 2019 - 2024. All rights reserved.