繁体   English   中英

用另一个txt文件的内容替换txt文件中的字符串(正则表达式)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM