How do I extract specific items from csv data and send them in the Gmail body (message)?

Asked 2 years ago, Updated 2 years ago, 97 views

The code may be different for what you want to do in the first place, but
The image is as follows.

csv data image
name,age,sex,tel,...

with open('sample.csv') asf:# csc file specified
reader=csv.reader(f)
For row in reader:
    data=row[0]+row[3]# csv to replace specific items (name and tel) with data

With this, only one line of data can be stored, right?
If there are 100 lines, I would like to extract all 100 lines of row[0]+row[3] and replace them with data.
Also, I would like to put the data substituted value in the body of gmail.

 from email.mime.text import MIMEText
import smtplib

account="[email protected]"
password="passpass"


to_email="Send 先@hoge.com"
from_email="Send 元@gmail.com"


subject="Test Mail"
message="Test Mail"#←I want to put data here
msg = MIMEText(message, "html")
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()

python google-api gmail

2022-09-29 22:49

1 Answers

Like this, you can add each line as a list and then connect it.
Specify or convert encoding if necessary, and take action when row[3] is not present.

data=[]
with open('sample.csv') asf:# csc file specified
    reader=csv.reader(f)
    For row in reader:
        Add specific items (name and tel) to data from data.append(row[0]+row[3])#csv

concatenate list of message='\n'.join(data)#data with new line


2022-09-29 22:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.