반복하고 Python을 사용하여 이메일을 보내기 위해 csv 파일을 만들고 있습니다. 코드는 저에게 효과적이지만 Google 스프레드 시트에서 데이터를 가져오고 일부 행을 건너 뛰고 반복하기 위해 너무 많은 코딩을하고 있다고 생각합니다.
내 코드는 다음과 같습니다.
import csv
import pandas as pd
import smtplib
from settings_djg import SENDER_EMAIL, SENDER_PASS # Email settings file
from datetime import date, datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from email.message import EmailMessage
from itertools import islice
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open("Hosting_Domains").sheet1
list_of_hashes = sheet.get_all_values()
new_list = pd.DataFrame(list_of_hashes)
# Here I'm using .iloc to skip some rows
new_list.iloc[[0,2,3,4,5,6,7,13,14,15,16,17,18,19,20]].to_csv('my_csv.csv', index=False, header=False)
msg = EmailMessage()
now = date.today()
# Here starts iteration
with open('my_csv.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
for row in csv_reader:
Google 스프레드 시트의 데이터는 다음과 같습니다.
X,DOMAIN,EXPIRE,OWNER,REGISTRANT,HOSTING
,,,CUSTOMERS,,,
B,company.com,2021-02-13,Customer,SITEGROUND,SITEGROUND
D,companyb.com,2021-05-17,Customer,GODDADY,GODDADY
E,companyc.com,2021-09-19,Customer,GODDADY,GODDADY
D,companye.com,2021-01-23,Customer,GODDADY,GODDADY
나는 당신의 목표를 다음과 같이 믿습니다.
iloc
파일로 사용 하고 저장 하지 않습니다 .[0,2,3,4,5,6,7,13,14,15,16,17,18,19,20]
로 시트 에서 행을 검색하려고합니다 .
0
포함됩니다. 그래서 0
1 열 이라고 생각했습니다 .[0,2,3,4,5,6,7,13,14,15,16,17,18,19,20]
배열의 인덱스가 언제 이면 list_of_hashes
필요한 행을을 사용하여 검색 할 수 있다고 생각합니다 [0,2,3,4,5,6,7,13,14,15,16,17,18,19,20]
.이 점을 스크립트에 반영하면 다음과 같이됩니다.
list_of_hashes = sheet.get_all_values()
values = [list_of_hashes[e] for e in [0, 2, 3, 4, 5, 6, 7, 13, 14, 15, 16, 17, 18, 19, 20]]
df = pd.DataFrame(values)
values
필요한 행을 검색 한 배열입니다.df
에서 변환 된 데이터 프레임입니다 values
.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다