Python attachemnt.encode('base64') 删除\n

Python attachemnt.encode('base64') removes \n

我正在使用 Odoo 并上传一个文本文件,我需要用 base64 对其进行编码,但编码会删除所有 '\n' 元素,然后我将所有内容都放在一行中。我怎样才能离开那些新行?

with open("Invoices.txt") as text_file:
                attachment=text_file.read()
                new_attach =attachment.encode('base64')

必须以 'rb' 而不是 'r'

打开文件