[英]Replacing string in txt file with content of another txt file (regular expressions)
我有兩個文件:“invoiceencoded.txt”(base64 代碼)和“invoice.txt”。 我想用第一個文本文件的 base64 代碼替換第二個文本文件中的“輸入”一詞。 目的是為其中的多個示例循環遍歷特定路徑,但這並不重要。 我有以下代碼:
import re
import os
for f_name in os.listdir('C:/..'):
if f_name.endswith('encoded.txt'):
fin = open(f_name, "rt")
filedata = fin.read()
with open(f_name[:-11]+".txt", 'r+') as f:
text = f.read()
text = re.sub('INPUT', filedata, text)
f.seek(0)
f.write(text)
f.truncate()
'INPUT' 字符串連接為 'abcINPUTdef'。 然而,而不是給我
“abc base64code def”,我得到:
"abc base64code
定義”
有誰知道如何刪除這個換行符?
提前致謝
換行符可能位於 invoiceencoded.txt 中 base64 字符串的末尾。
我建議您刪除這些換行符並重新運行您的腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.