I would like to extract only the csv date and content column, extract only the column containing only yesterday's date, and send an email.
I wrote the code below, but an error occurred.
I think it's because if dataframe is empty, it gets an error.
c Create a Pandas DataFrame that extracts only a specific column of csv
② Extract only yesterday's columns
③ For the first and second lines of the created DataFrame (index is 0 and 1) processing from ~ to の
④ Extract column names and data for each column of the row to be processed, add a new line code to each column, and concatenate
⑤ Each extracted and concatenated column name + data is further concatenated with a new line code
⑥ Insert and send concatenated data as the body of the email
CSV Data
Date Content CD
Sunday, February 7, 2021 11:10:00 test 2 test 3 test 4
Saturday, February 6, 2021 20:00:00 test 5 test 6 test 7
February 10, 2021 (Wed) 15:59:00 test 8 test 9 test 10
I'd like to send you an email with only yesterday's columns.
Date Contents
Wednesday, February 10, 2021 at 15:59:00 test 8
Current Code
Gmail
from datetime import datetime as dt, date, timedelta
import csv
import pandas aspd
from email.mime.text import MIMEText
import smtplib
today=dt.today()
(dt.strftime(today, '%Y/%m/%d')
yesterday=today-timedelta(days=1)
print(yesterday.strftime("%Y / %m / %d"))
# Loaded csv data into df.
filename = "test.csv"
df=pd.read_csv(filename, encoding='shift-jis')
#### df_i = df.set_index("A", "B", "C")
df_i = df [["Date", "Content"]]
print(df_i)
df_h=df [df["date"].str.contains(yesterday)]
print(df_h)
# So far, I can get yesterday's data well.
account="[email protected]"
password="123"
to_email="123"
from_email="[email protected]"
for jin range(2):
subject="[TEST]"
message='\n'.join([f"{column}\n{data}\n"for column, data indf_h.iloc[j].iteritems()])####!
print(message)
msg = MIMEText(message)
msg ["Subject"] = subject
msg["To"] = to_email
msg["From"] = from_email
server=smtplib.SMTP ("smtp.gmail.com", 587)
server.starttls()
server.login(account, password)
server.send_message(msg)
server.quit()
Error Code
File"c:/Users/test/Documents/S3/test.py", line 34, in<module>
message='\n'.join([f"{column}\n{data}\n"for column, data indf_h.iloc[j].iteritems()])
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 879, in__getitem__
return self._getitem_axis(maybe_callable,axis=axis)
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1496, in_getitem_axis
self._validate_integer(key,axis)
File "C:\Users\test\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1437, in_validate_integer
raise IndexError ("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
Sorry for the inconvenience, but I would appreciate it if you could reply.
Thank you for your cooperation.
python pandas csv gmail
before answering:
"The following are the confirmations regarding the ""Conditions 3 to 5 cannot be met"" written in the comment."
The first line of the question, "I want to extract only columns containing only yesterday's dates and send emails," is probably the mistake of "I want to extract only rows containing only yesterday's dates and send emails."
In the same way, "I want to get only yesterday's column and send an email" in the middle is "I want to get only yesterday's row and send an email."
Also, the contents of the text of the email were written in the questionnaire as follows:
Date Contents
Wednesday, February 10, 2021 at 15:59:00 test 8
The content indicated in the Current Code is in line with another previous question:
message='\n'.join([f"{column}\n{data}\n"for column, dataindf_h.iloc[j].iteritems()])####!
If you apply it, it will look like this:
Date
Wednesday, February 10, 2021 at 15:59:00
Contents
test8
Which or other content do you really want from the body of the email?
What are the known issues at this time:
As @metropolis commented, the process for extracting yesterday's data is not working."#So far, yesterday's data can be retrieved well" is either a confirmation error or a transcription error when entering a question.
This line:
yesterday=today-timedelta(days=1)
print(yesterday.strftime("%Y / %m / %d"))
To do this:
yesterday=(today-timedelta(days=1)).strftime("%Y / %m / %d")
print(yesterday)
This line is:
df_h=df [df["date"].str.contains(yesterday)]
You should do this:
df_h=df_i [df_i["date"].str.contains(yesterday)]
Similarly, as @metropolis commented, "I think it's because in iloc, if dataframe is empty, it gets an error." The reason for this is that it "extracts only lines containing yesterday's dates" so it's not necessarily two lines of data, but it's still looping in the answer to the previous question.
Dear @metropolis, As you can see in the comments, this line is:
for jin range(2):
You should do this:
for jin range(len(df_h)):
Also, you may need to change the following line to match the content of the email body you are trying to print this time:
message='\n'.join([f"{column}\n{data}\n"for column, dataindf_h.iloc[j].iteritems()])####!
Also, it is necessary to detect "no line with yesterday's date" in the first place and close it before processing the email.
© 2024 OneMinuteCode. All rights reserved.