猿问

查找字符串的条件

我试图手动输入一个 Carmodel,当它在定义的区域内找到时,在第一个区域内寻找另一个名为“Date”的子区域,然后在它之后添加带有以下字符串“For rent”的行。像这样的东西(请看下面用 # 突出显示的预期输出,在 example.txt 中)


#Region1 starts here

Carname 

"Ford Ranger"

Color "Red"

Mileage "1024"

#Subregion1 starts here

Date

11/02/2018

/

#Subregion1 ends here

#Region1 ends here

#Add line here "For rent"

#Region2 starts here

Carname 

"Toyota Prius"

Color "Red"

Mileage "1024"

#Subregion2 starts here

Date

10/06/2019

/

#Subregion2 ends here

#Region2 ends here

#Add line here "For rent"

此列表在 .txt 文件中持续了数百个条目,每个 Carname 代表一个具有自己的子区域的新区域。


但它应该一次只定位 1 个,因为它应该寻找手动用户输入。对于上面的例子,手动输入将是:“Ford Ranger”


这是我尝试过的,但我被卡住了:


transitions = dict()

in_region = False

reg_end = -1

current_title = None

carmodel = input("input carmodel:")


with open("example.txt","r") as testfile:

    content = testfile.readlines()


for idx, line in enumerate(content):

    if line.startswith('Carname'):

    #if line.startswith ('Date') within the input 'Carmodel'

    #if line.endswith ('/') within the Date region

        # Commit last transition before this to dict, if any

        if current_title:

            transitions[reg_end] = current_title

        # add suffix for printing

        current_title = 'For rent\n'

    elif line.strip().startswith(carmodel):

        in_region = True

        # This will be overwritten while we remain in the region

        reg_end = idx

    elif in_region:

        in_region = False


if current_title:

    transitions[reg_end] = current_title


with open("example.txt", "w") as output:

    for idx, line in enumerate(content):

        output.write(line)

        if idx in transitions:

            output.write(transitions[idx])

任何帮助,将不胜感激。谢谢!


qq_笑_17
浏览 118回答 1
1回答

慕容森

你可以尝试这样的事情。transitions = dict()in_region = Falsereg_end = -1current_title = Nonecarmodel = input("input carmodel:")with open("example.txt","r") as testfile:    content = testfile.readlines()find_date_line=Falsefor idx, line in enumerate(content):    if find_date_line:        if line.strip().startswith('/'):            reg_end = idx            find_date_line = False    else:        if line.startswith('Carname'):        #if line.startswith ('Date') within the input 'Carmodel'        #if line.endswith ('/') within the Date region            # Commit last transition before this to dict, if any            if current_title:                transitions[reg_end] = current_title            # add suffix for printing            current_title = 'For rent\n'        elif line.strip().startswith(carmodel):            in_region = True            # This will be overwritten while we remain in the region            # reg_end = idx            find_date_line=True        elif in_region:            in_region = Falseif current_title:    transitions[reg_end] = current_titlewith open("example.txt", "w") as output:    for idx, line in enumerate(content):        output.write(line)        if idx in transitions:            output.write(transitions[idx])当你提供输入时"Ford Ranger"输出文件会像#Region1 starts hereCarname "Ford Ranger"Color "Red"Mileage "1024"#Subregion1 starts hereDate11/02/2018/For rent#Subregion1 ends here#Region1 ends here#Add line here "For rent"#Region2 starts hereCarname"Toyota Prius"Color "Red"Mileage "1024"#Subregion2 starts hereDate10/06/2019/#Subregion2 ends here#Region2 ends here#Add line here "For rent"
随时随地看视频慕课网APP

相关分类

Python
我要回答