亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

查找字符串的條件

查找字符串的條件

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 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號