【问题标题】:How to find and replace words in a python file?如何查找和替换 python 文件中的单词?
【发布时间】:2022-11-19 22:58:32
【问题描述】:

有一个模板文件:

`ZOYX:姓名:IUA:S:BCSU,麻木:快速;

佐伊普:IUA:姓名:“ip1",,49155:"ip2",30,,,49155;

ZDWP:名字: BCSU,麻木:0,3:姓名;

ZOYS:IUA:姓名:行为;

ZERC:BTS=58,TRX=数字::FREQ=567,TSC=0,:DNAME=姓名:CH0=TCHD,CH1=TCHD,CH2=TCHD,CH3=TCHD,CH4=TCHD,CH5=TCHD,CH6=TCHD,CH7=TCHD:;

ZERM:防弹少年团=58,TRX=数字:LEV=-91;

ZERM:防弹少年团=58,TRX=数字:PREF=N;

ZERS:防弹少年团=58,TRX=数字:你;`

在其中,您需要更换数字,姓名,麻木, _ ip1_,ip2, 以及用户输入的值。 我就是这样做的:

`

repeat="y"
while repeat == "y":

    keys=['_ip1_', '_ip2_', '_sName_', '_sNumb_', '_tNumb_']
    print(keys) 
    #print(keys[2])
    print("+++++++++++++++++++++++++++++1")

    values=[]
    #ip1, ip2, sName, sNumb, tNumb = input("Enter the IP address1: "), input("Enter the IP address2: "), input("Enter the station name: "), input("Enter the station number: "), input("Enter the transmitter number: ")
    ip1, ip2, sName, sNumb, tNumb = 1111, 2222, 3333, 4444, 5555
    
    values.append(ip1)
    values.append(ip2)
    values.append(sName)
    values.append(sNumb)
    values.append(tNumb)
    print(values)
    #print(values[2])
    print("+++++++++++++++++++++++++++++2")

    dictionary={}
    for i in range(len(keys)):
        dictionary[keys[i]] = values[i]
        search_text = dictionary[keys[i]]
        replace_text = keys[i]
        print(search_text)
        print(replace_text)
        print("+++++++++++++++++++++++++++++3")


    with open(r'template.txt', 'r') as oFile:
        rFile = oFile.read()
    #print(rFile)
    with open(r'output.txt', 'a') as wFile:
        wFile.write('\n')
        wFile.write('\n')
        wFile.write('\n')
        wFile.write(rFile)





    repeat = input("Do you want to continue? (y/n): ")
    if repeat == "n":
        break
    while (repeat!="y" and repeat!="n"):
        repeat = input("Please enter the correct answer (y/n): ")

` 我只有重复输出文件中显示的文本。我如何找到并更改为正确的词?

我只有重复输出文件中显示的文本。我如何找到并更改为正确的词?我希望在输出文件中得到这个:

`ZOYX:33333:IUA:S:BCSU,55555:AFAST;

ZOYP:IUA:33333:"1111",,49155:"2222",30,,,49155;

ZDWP:33333:BCSU,55555:0,3:33333;

ZOYS:IUA:33333:ACT;

ZERC:BTS=58,TRX=3::FREQ=567,TSC=0,:DNAME=33333:CH0=TCHD,CH1=TCHD,CH2=TCHD,CH3=TCHD,CH4=TCHD,CH5=TCHD,CH6= TCHD,CH7=TCHD:;

ZERM:BTS=58,TRX=4444:LEV=-91;

ZERM:BTS=58,TRX=4444:PREF=N;

ZERS:BTS=58,TRX=4444:U;`

【问题讨论】:

    标签: python


    【解决方案1】:

    如果您使用 vs-code 作为您的 IDE,您可以通过选择它并按 ctrl+shift+r 来重构您想要的单词或变量。或者您可以通过选择它并按 ctrl+f2 来选择重命名它的所有出现。

    如果您使用 PyCharm 作为 IDE,您可以选择变量并按 shift+f6 重构变量。

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 1970-01-01
      • 2021-12-26
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 2021-01-12
      相关资源
      最近更新 更多