为了账号安全,请及时绑定邮箱和手机立即绑定

查找字符串的条件

查找字符串的条件

qq_笑_17 2023-05-23 16:12:59
我试图手动输入一个 Carmodel,当它在定义的区域内找到时,在第一个区域内寻找另一个名为“Date”的子区域,然后在它之后添加带有以下字符串“For rent”的行。像这样的东西(请看下面用 # 突出显示的预期输出,在 example.txt 中)#Region1 starts hereCarname "Ford Ranger"Color "Red"Mileage "1024"#Subregion1 starts hereDate11/02/2018/#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"此列表在 .txt 文件中持续了数百个条目,每个 Carname 代表一个具有自己的子区域的新区域。但它应该一次只定位 1 个,因为它应该寻找手动用户输入。对于上面的例子,手动输入将是:“Ford Ranger”这是我尝试过的,但我被卡住了:transitions = dict()in_region = Falsereg_end = -1current_title = Nonecarmodel = 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 = 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])任何帮助,将不胜感激。谢谢!
查看完整描述

1 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

你可以尝试这样的事情。


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()

find_date_line=False

for 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 = 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])

当你提供输入时


"Ford Ranger"

输出文件会像


#Region1 starts here

Carname 

"Ford Ranger"

Color "Red"

Mileage "1024"

#Subregion1 starts here

Date

11/02/2018

/

For rent

#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"


查看完整回答
反对 回复 2023-05-23
  • 1 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号