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

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

Python:如何讀取段落的每一行并復(fù)制具有特定單詞的行

Python:如何讀取段落的每一行并復(fù)制具有特定單詞的行

躍然一笑 2023-12-29 15:32:44
我需要閱讀一個(gè)段落,我需要復(fù)制只有關(guān)鍵字的行。段落是這樣的:aaaaaaaaaaa[new,aaa] < name of the file with path ] //asabbsjkorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap[mod] <name of the file with path 我的算法:if keyword exists in the line :      copy that line else:       leave it
查看完整描述

2 回答

?
寶慕林4294392

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

嘗試這幾行代碼:


copy_lines= []

for line in paragraph.split('\n'):

    if keyword in line:

        copy_lines.append(line)

print(copy_lines)

或一張內(nèi)襯:


copy_lines= [line for line in paragraph.split('\n') if keyword in line]


查看完整回答
反對(duì) 回復(fù) 2023-12-29
?
Helenr

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

如果您嘗試從文件中讀取該段落:


keyword = "your_keyword"


with open("demofile.txt", "r") as f:

    line_list = f.readlines()

    magic_lines = [line for line in line_list if keyword in line]


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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