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

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

正則表達(dá)式搜索自動(dòng)化無(wú)聊的東西,功能和錯(cuò)誤

正則表達(dá)式搜索自動(dòng)化無(wú)聊的東西,功能和錯(cuò)誤

慕田峪4524236 2022-08-11 17:19:58
我已經(jīng)為此掙扎了幾個(gè)小時(shí):import os, sys, reprint('Type the path to the folder.')input_path = input()input_path = os.path.join(input_path)print('Select the search term')input_term = input()search_regex = re.compile(input_term)all_files = os.listdir(input_path)for j in range(len(all_files)):    new_path = os.path.join(input_path, all_files[j])    search_regex = re.compile(input_term)    target_file = open(new_path, 'r')    file_content = target_file.read()    file_content_in_list = search_regex.findall(file_content)    print('A grand total of ' + str(len(file_content_in_list)) + ' items were found at ' + str(new_path))這按預(yù)期工作。該代碼讀取給定文件夾中的所有文件,并檢查找到搜索詞的次數(shù)。但是,當(dāng)我嘗試將代碼的不同部分定義為函數(shù)時(shí),我只得到錯(cuò)誤:import os, sys, redef select_folder():    print('Type the path to the folder.')    input_path = input()    input_path = os.path.join(input_path)def select_all_files():    all_files = os.listdir(input_path)    for j in range(len(all_files)):        search_a_file()def ask_for_regex():    print('Select the search term')    input_term = input()    search_regex = re.compile(input_term)def search_a_file():    new_path = os.path.join(input_path, all_files[j])    search_regex = re.compile(input_term)    target_file = open(new_path, 'r')    file_content = target_file.read()    file_content_in_list = search_regex.findall(file_content)    print('A grand total of ' + str(len(file_content_in_list)) + ' items were found at ' + str(new_path))select_folder(): #Syntax Error here!    ask_for_regex():        select_all_files():# if i put them like this then everything break down. I guess the variables are forgotten?select_folder()ask_for_regex()select_all_files()我想這個(gè)錯(cuò)誤是顯而易見(jiàn)的,但我無(wú)法理解它......
查看完整描述

2 回答

?
子衿沉夜

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

正如其他人所提到的,您需要對(duì)從函數(shù)返回值進(jìn)行一些讀取,然后可以在其他區(qū)域使用。我重新格式化了你的代碼,以顯示它可能是什么樣子,看看你是否能挑出所做的更改。我重命名了一些函數(shù),以便更好地描述它們的作用。另外,我沒(méi)有正式測(cè)試這段代碼,但它應(yīng)該讓你很好地了解你需要采取什么方向。


另外,仍然試圖獲得聲譽(yù)點(diǎn),所以如果它有幫助,請(qǐng)將答案標(biāo)記為已接受!


import os

import re



def get_folder_path():

    input_path = input('Type the path to the folder: ')

    return os.path.join(input_path)



def set_regex():

    input_term = input('Type the regex to search for: ')

    return re.compile(input_term)



def search_for_files():

    path = get_folder_path()

    search_regex = set_regex()

    file_list = os.listdir(path)

    count = 0

    for file in file_list:

        with open(file, 'r') as f:

            file_content = f.read()

            match = re.search(search_regex, file_content)

            if match:

                count += 1


    print('A grand total of ' + str(count) + ' items were found at ' + str(path))



search_for_files()


查看完整回答
反對(duì) 回復(fù) 2022-08-11
?
白衣染霜花

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

下面是一個(gè)代碼示例,類似于您的原始代碼,它以線性方式計(jì)算直角三角形的斜邊:


import math

a = 3

b = 4

c = math.sqrt(a**2 + b**2)

以下是等效的代碼,重新編寫(xiě)以使其更易于組合,更加模塊化:


import math


def hypotenuse(a, b):

    return math.sqrt(a**2 + b**2)


c = hypotenuse(3, 4)


查看完整回答
反對(duì) 回復(fù) 2022-08-11
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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