Home > Back-end >  Keep String Formatted When Sending it via Email [PYTHON]
Keep String Formatted When Sending it via Email [PYTHON]

Time:12-05

I have a string when it's printed in my terminal, it looks the way I want, but when I use the variable to send it via email, it looses all its formatting. Is there anyway I can fix this?

This is how it looks on the terminal This is how it looks on email

This is how I am declaring and printing for the first image:

ticket_medio = (faturamento['Valor Final'] / quantidade['Quantidade']).to_frame()
print(ticket_medio)

And this is how I am doing the formatted string that is being sent to the user:

body_email = f'Segue o ticket médio deste mês:\n\n{ticket_medio}'

CodePudding user response:

To save a pandas dataframe named ticket_medio to a CSV file, you can use the to_csv method. Here is an example of how to do this:

# Import the pandas library
import pandas as pd

# Save the dataframe to a CSV file
ticket_medio.to_csv('ticket_medio.csv')

This will create a CSV file named ticket_medio.csv in the current directory. The CSV file will contain the data from the ticket_medio dataframe.

Once the CSV file has been created, you can attach it to an email and send it to the desired recipient. The recipient can then open the CSV file in a program like Microsoft Excel or Google Sheets to view the data.

Alternatively, you can also specify a different file path to save the CSV file to a different location on your computer, or you can specify additional options to customize the way the data is saved to the CSV file. For more information, you can refer to the documentation for the to_csv method.

CodePudding user response:

You can use pandas.DataFrame.to_string.

Render a DataFrame to a console-friendly tabular output.

Try this :

body_email = '''Segue o ticket médio destemês:\n\n{}'''.format(ticket_medio.to_string())
  • Related