【问题标题】:sending email but eliminating 'L' in the string from python发送电子邮件但从 python 中删除字符串中的“L”
【发布时间】:2017-08-09 14:36:17
【问题描述】:

我的输出来了:临时表中的总记录是 (3L,) 我只想要 n 号括号,我怎样才能删除 L , 我将查询转换为 str 因为我无法连接 part1 和 msg

#!/usr/bin/env python  
import csv
import MySQLdb
import pysftp
import smtplib
import sys

#connection to Database
conn =  MySQLdb.connect(host="XXXXX", # The Host
                      user="XXXXX", # username
                      passwd="XXXXX", # password
                      db="XXXXX") # name of the data base
part1 = """From: XXXXX
To: To Person <XXXXX.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
Toatal records in Staging Table inserted:
"""
sender = 'XXXXX'
receivers = ['XXXXX','XXXXX']

x = conn.cursor()
query = "select count(*) as toatal_records_inserted from fpwbs_feed_stg"
x.execute(query)
msg = ""
for row in x.fetchall():
    msg += str(row) 
msg=part1+msg
smtpObj=smtplib.SMTP('XXXXX', 25)
smtpObj.sendmail(sender, receivers, msg) 

【问题讨论】:

    标签: python mysql email


    【解决方案1】:

    这样做可以获得元组中的第一项:

    for row in x.fetchall():
        msg += str(row[0]) 
    

    【讨论】:

    • 最好只抓取除最后一个字符之外的所有内容,因为该数字可能超过 1 个数字:msg += str(row[:-1])
    • @user2896976:它在一个元组中,所以这只会从元组中提取整数。
    • 抱歉没有看到它在一个元组中。然后使用row[0][:-1]
    • @user2896976:如果你这样做str(3L),你会得到'3'
    • 但是如果它有一个L 那么它已经是一个字符串了。并且3L 在 Python 中无效,与 C 不同。
    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 2017-08-17
    • 2012-02-08
    • 2021-08-13
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多