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

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

如何映射文件中的不同文本

如何映射文件中的不同文本

互換的青春 2023-05-23 16:48:03
代碼如下我需要用字典替換文件中的文本abc.txt 包含this is abc filecde.txt 包含this is cde file    {'abc':'123', 'cde':'456'}    all_files = ['abc.txt','cde.txt']    out_files = ['abc_output.txt','cde_output.txt']    read_dict = {}    for in_f, out_f in zip(all_files, out_files):        #print (in_f, out_f)        with open(in_f,'r') as read_file:            lines = read_file.readlines()        with open(out_f,'w+') as write_file:             #write_file.writelines(lines)            print(lines)我的出去['This is abc file']['This is cde file']預(yù)計出來['This is 123 file']['This is 456 file']
查看完整描述

2 回答

?
智慧大石

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

mapping = {'abc':'123', 'cde':'456'}

all_files = ['abc.txt','cde.txt']

out_files = ['abc_output.txt','cde_output.txt']

read_dict = {}

for in_f, out_f in zip(all_files, out_files):

    #print (in_f, out_f)

    with open(in_f,'r') as read_file:

        lines = read_file.readlines()

    with open(out_f,'w+') as write_file:

        for line in lines:

            for key in mapping:

                line = line.replace(key, mapping[key])

            #write_file.writelines(line)


查看完整回答
反對 回復(fù) 2023-05-23
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個贊

你也可以這樣做


mapping = {'abc':'123', 'cde':'456'}

all_files = ['abc.txt','cde.txt']

out_files = ['abc_output.txt','cde_output.txt']

read_dict = {}

for in_f, out_f in zip(all_files, out_files):

? ? with open(in_f,'r') as read_file:

? ? ? ? lines = read_file.readlines()

? ? with open(out_f,'w+') as write_file:? ? ? ? ?

? ? ? ? for line in lines:? ??

? ? ? ? ? ? for key in mapping:

? ? ? ? ? ? ? ? line = line.replace(key, mapping[key])

? ? ? ? ? ? #write_file.writelines(line)


查看完整回答
反對 回復(fù) 2023-05-23
  • 2 回答
  • 0 關(guān)注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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