如何删除文本的段落?

发布于 2025-02-09 21:55:22 字数 1174 浏览 4 评论 0原文

我成功地创建了一个发送多个电子邮件的程序,但是我没有成功地正确格式化文本。

每当我尝试创建新行时,Python都会自动插入一个新段落,而我不想要。我试图将.lstrip放在文本块的ENT之后,但在“”但没有起作用之后。

文本应该是这样的:

"Dear owner,
 Our records indicate that your payment  have not been received so far. 
 The total due is:"

但是在电子邮件中打印出来:

"Dear owner
    Our records indicate that your payment  have not been received so far. 
    The total due is:"

请参阅下面的代码,谢谢对于任何输入。

import email
import win32com.client as client
import pandas as pd

tabela = pd.read_excel('over60.xlsx')
#print(tabela)

data = tabela[['Customer','Email','Owner','Total Due']].values.tolist()

for dado in data:

    owner_number = dado[0]
    to = dado[1]
    owner = dado[2]
    amount = dado[3]

    outlook = client.Dispatch('Outlook.Application')
    account = outlook.session.Accounts['myemail.com']

    email = outlook.CreateItem(0)
    email.To = 'myemail.com'
    email.Subject = f'Past Due Notice - {owner} - {owner_number}'
    email.Body =f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}.

I have succesfully created a program that send multiple emails, but I'm not having success to format the text correctly.

Every time that I try to create a new line, python automatically inserts a new paragraph and I don't want that. I have tried to place .lstrip at the ent of the text block, after the """ but didn't work.

The text should be like this:

"Dear owner,
 Our records indicate that your payment  have not been received so far. 
 The total due is:"

But is being printed on the email like this:

"Dear owner
    Our records indicate that your payment  have not been received so far. 
    The total due is:"

Please see my code below, thanks for any inputs.

import email
import win32com.client as client
import pandas as pd

tabela = pd.read_excel('over60.xlsx')
#print(tabela)

data = tabela[['Customer','Email','Owner','Total Due']].values.tolist()

for dado in data:

    owner_number = dado[0]
    to = dado[1]
    owner = dado[2]
    amount = dado[3]

    outlook = client.Dispatch('Outlook.Application')
    account = outlook.session.Accounts['myemail.com']

    email = outlook.CreateItem(0)
    email.To = 'myemail.com'
    email.Subject = f'Past Due Notice - {owner} - {owner_number}'
    email.Body =f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只等公子 2025-02-16 21:55:22

多行字符串包括在行开始时的缩进,请尝试在屏幕左侧的线路开始,或用\ n eg替换新

f"""Dear {owner},\n\nOur records indicate that your payment  have not been received so far.\n\nThe total due is ${amount: .2f}.

线n 呈现为新线,而不是字面字符“ \ n”

The multiline string is including the indent at the start of the line, try unindenting the start of the line all the way to the left of the screen, or replacing newlines with \n e.g:

f"""Dear {owner},\n\nOur records indicate that your payment  have not been received so far.\n\nThe total due is ${amount: .2f}.

\n renders as a newline and not the literal characters "\n"

家住魔仙堡 2025-02-16 21:55:22

使用“”“”保留Whitespace,因此像这样的字符串

"""Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

实际上将这些领先的空间放入。

如果您想继续使用多行字符串,则可以应用 ixplast.spection.cleandoc这...

import inspect

# ... your code ...

body = f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

email.Body = inspect.cleandoc(body)

Using """ preserves whitespace so a string like this...

"""Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

Actually puts those leading spaces in.

If you want to keep using a multi-line string, you can apply something like inspect.cleandoc to clean up the string, like this...

import inspect

# ... your code ...

body = f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

email.Body = inspect.cleandoc(body)
帥小哥 2025-02-16 21:55:22

您可以写出多行字符串,例如:(

>>> string = (
    'line1\n'
    'line2\n')

像元组一样,但没有逗号),

并且它可以打印

>>> print(string)
line1
line2

您可以正确保留代码标识

You can write your multiline string like:

>>> string = (
    'line1\n'
    'line2\n')

(like a tuple but without commas)

and it prints

>>> print(string)
line1
line2

moreover you can keep your code identation correctly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文