第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在 python 中使用“類”搜索多個(gè)字符串

在 python 中使用“類”搜索多個(gè)字符串

嚕嚕噠 2022-07-19 20:10:45
我有這個(gè)代碼,它可以找到單詞 |1234| 并復(fù)制所有行直到 abcd| 找到,它不會復(fù)制包含 abcd| 的行。我想讓這段代碼適用于多個(gè)字符串。例如,搜索該行是否包含 |1234| 或 |56789| 或 |54321| 并復(fù)制所有包含這些單詞的行,直到行 abcd| (這不會改變)。順便說一句,代碼還將所有 txt 合二為一(我需要搜索多個(gè)文件。是否可以修改代碼使其可以搜索多個(gè) start_marker?如何?有人可以幫我嗎?import globimport osclass Collector:    def __init__(self, filename, start_marker, stop_marker):        self.toggle_collect = False        self.f = open(filename, encoding='utf-8', errors='ignore')        self.m1 = '|1234|'        lf.m2 = 'abcd|'    def __enter__ (self):        return self    def __exit__ (self, exc_type, exc_value, traceback):        self.f.close()    def __iter__(self):        return self    def __next__(self):        while True:            r = next(self.f)            if self.m1 in r:  # found the start-collecting marker                self.toggle_collect = True            elif self.m2 in r:  # found the stop-collecting marker                self.toggle_collect = False                continue            if self.toggle_collect:  # we are collecting                return r  # .rstrip()  # provide rowsrc_path = "e:/teste/Filtrados/"dst_path = "e:/teste/FiltradosFinal/"filelist = (fn for fn in os.listdir(src_path) if fn.endswith(".txt"))for x in filelist:    print(f"Processing file {x}")    with open(os.path.join(dst_path, x), 'w', encoding='utf-8', errors='ignore') as f, \         Collector(os.path.join(src_path, x), '1234', 'abcd') as c:        for r in c:            f.write(r)read_files = glob.glob("e:/teste/FiltradosFinal//*.txt")with open("e:/teste/teste.txt", "wb") as outfile:    for f in read_files:        print("Combinando arquivos")        with open(f, "rb") as infile:輸入00000|12333|somethingwordanother_oneabcd|00000|12320|somethingwordanother_oneabcd|00000|12321|somethingwordanother_oneabcd|輸出00000|12333|somethingwordanother_oneabcd|00000|12321|somethingwordanother_oneabcd|我想復(fù)制所有,例如,'|12333|' 之間的行 或“|12321|” 直到找到結(jié)束標(biāo)記 abcd|。我的代碼做到了,但只有當(dāng)我設(shè)置一個(gè)標(biāo)記時(shí),我才想要多個(gè)開始標(biāo)記
查看完整描述

1 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

import glob

import os



class Collector:

    def __init__(self, filename, start_marker, stop_marker):

        self.toggle_collect = False

        self.f = open(filename, encoding='utf-8', errors='ignore')

        self.m1s = ['|1234|', 'other_word1', 'other_word2']

        self.m2 = 'abcd|'


    def __enter__(self):

        return self


    def __exit__(self, exc_type, exc_value, traceback):

        self.f.close()


    def __iter__(self):

        return self




    def __next__(self):

        while True:

            r = next(self.f)

            for m1 in self.m1s:

                if self.m2 in r:  # found the stop-collecting marker

                   break

                if m1 in r:  # found the start-collecting marker

                   return r  # .rstrip()  # provide row




src_path = "e:/teste/Filtrados/"

dst_path = "e:/teste/FiltradosFinal/"

filelist = (fn for fn in os.listdir(src_path) if fn.endswith(".txt"))

for x in filelist:

    print(f"Processing file {x}")

    with open(os.path.join(dst_path, x), 'w', encoding='utf-8', errors='ignore') as f, \

            Collector(os.path.join(src_path, x), '1234', 'abcd') as c:

        for r in c:

            f.write(r)


read_files = glob.glob("e:/teste/FiltradosFinal//*.txt")

with open("e:/teste/teste.txt", "wb") as outfile:

    for f in read_files:

        print("Combinando arquivos")

        with open(f, "rb") as infile:

我沒有測試代碼,因?yàn)槟鷽]有提供任何方便的輸入或所需的輸出,但這似乎可以滿足您的要求。


我建議使用有意義的名稱而不是m1and m2,這樣您就可以在代碼變大時(shí)調(diào)試自己的代碼(更不用說讓其他人閱讀它了)。


查看完整回答
反對 回復(fù) 2022-07-19
  • 1 回答
  • 0 關(guān)注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號