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

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

匹配 2 個文本文件中的文本并從第一個文件獲取信息并附加到另一個文件

匹配 2 個文本文件中的文本并從第一個文件獲取信息并附加到另一個文件

溫溫醬 2023-08-08 17:27:37
我有 2 個文本文件。第一個文件包含文本的元數據信息(如字體大小等),第二個文件僅包含文本。我需要匹配兩個文件之間的文本,從第一個文件中獲取元數據信息并將其添加到第二個文件中。例如,文件A數據:[Base Font : PSJEPX+Muli-Light, Font Size : 7.5, Font Weight : 300.0]We are not satisfied with our 2018 results. We have the global footprint, assets and team to [Base Font : SVTVFR+Muli-Light, Font Size : 7.5, Font Weight : 300.0] [Base Font : PSJEPX+Muli-Light, Font Size : 7.5, Font Weight : 300.0]perform better. We have made a number of changes to position for sustainable growth.New line that does not start with square brackets.[Base Font : SVTVFR+Muli-SemiBold, Font Size : 8.1, Font Weight : 600.0]Innovation文件 B 數據:We are not satisfied with our 2018 results. We have the global footprint, assets and team to perform better. We have made a number of changes to position for sustainable growth.New line that does not start with square brackets.Innovation預期輸出:[Base Font : PSJEPX+Muli-Light, Font Size : 7.5, Font Weight : 300.0]We are not satisfied with our 2018 results. We have the global footprint, assets and team to perform better. We have made a number of changes to position for sustainable growth.New line that does not start with square brackets.[Base Font : SVTVFR+Muli-SemiBold, Font Size : 8.1, Font Weight : 600.0]Innovation因此,基本上,只有當元數據信息發生更改時,“文件 A”中的元數據才必須附加到“文件 B”。我的方法: def readB(x):     with open(File B) as resultFile:         for line in resultFile:             if x in line:                 print(x)def readA():    with open(File A) as bondNumberFile:        for line in bondNumberFile:            readB(line)readA()我的問題是,我不確定如何從文件 A 中獲取元數據信息并將其附加到文件 B。此外,我的代碼在匹配文本時無法處理元數據信息(方括號內)。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

請嘗試下面的程序。該程序首先讀取filea并創建樣式和線條的字典,然后逐行讀取fileb以從字典中匹配和選擇樣式,并將其寫入到filec.


import re

table={}

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

    for line in f:

        if line.strip():

            parts=re.findall("^(\[.*?\])?(.*)$",line)[0]

            if parts[0] in table:

                table[parts[0]]+=parts[1]

            else:

                table[parts[0]]=parts[1]

with open("fileb.txt","r") as f, open("filec.txt","w") as f1:

    for line in f:

        if line.strip():

            for i in table:

                if line.strip() in table[i]:

                    f1.write(i+line)

                    break

                else:

                    pass

        else:

            f1.write(line)

輸出


[Base Font : PSJEPX+Muli-Light, Font Size : 7.5, Font Weight : 300.0]We are not satisfied with our 2018 results. We have the global footprint, assets and team to perform better. We have made a number of changes to position for sustainable growth.

New line that does not start with square brackets.


[Base Font : SVTVFR+Muli-SemiBold, Font Size : 8.1, Font Weight : 600.0]Innovation


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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