我目前正在学习python。我正在处理电子邮件脚本。发送邮件进展顺利,我只想将数据添加到数据库中的邮件中。他这样做,他只是再次把它作为一个长长的清单给出。喜欢:
[(u'Test test', ), (u'i', ), (u'k', ), (u'b', ), (u'e', ), (u'n', ), (u'j', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', )]
他还给出字符串为:uTEXT 而不是 TEXT。
在几个网站上,我已经看过如何编码等,我只是没有工作。我如何让它工作?对于你们大多数人来说,这可能是一件非常简单的事情。
我还想显示下面列表中的值并解决。不带字符列表 -> (["文本"])
#!/usr/bin/python
import smtplib
import datetime
import time
import locale
import pyodbc
# DB
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=local;Integrated Security=SSPI;')
cursor = cnxn.cursor()
cursor.execute("select recordname from tablename where cast(Date as date) = cast(getdate() as date)");
row = cursor.fetchall()
datum = datetime.date.today().strftime("%Y-%d-%w")
# Datum notatie
date = (datetime.date.today() - datetime.timedelta(1)).strftime("%d-%w-%Y")
# SMTP connectie
server = "smtp.gmail.com"
port = 587
# login G-mail
login = "x"
password = "x"
# Mail instellingen + onderwerp en berichttekst
sender = "name"
recipient = "email"
subject = "subject"
for rows in row:
print rows
message = "text" + str(rows) + "text"
headers = ["From: " + sender,
"Subject: " + subject + " " + date,
"To: " + recipient,]
headers = "rn".join(headers)
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(login, password)
session.sendmail(sender, recipient, headers + "rnrn" + message)
session.quit()
我的查询和行的结果是这样的,我将在我的邮件中看到这个:
[(u'Test test', ), (u'i', ), (u'k', ), (u'b', ), (u'e', ), (u'n', ), (u'j', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', )]
我尝试过这样的事情:
message = "text" + str(rows.encode('ascii') + "text
或:
[(rows[0].encode("utf-8"),) for rows in row]:
print rows
对于所有这些事情,我都会得到一个错误。我认为我的转换数据的方式不好。或者我没有在正确的时间适用。
我的查询和行的结果是这样的,我将在我的邮件中看到这个:
[(u'Test test', ), (u'i', ), (u'k', ), (u'b', ), (u'e', ), (u'n', ), (u'j', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', )]
我尝试过这样的事情:
message = "text" + str(rows.encode('ascii') + "text
或:
[(rows[0].encode("utf-8"),) for rows in row]:
print rows
对于所有这些事情,我都会得到一个错误。我认为我的转换数据的方式不好。或者我没有在正确的时间适用。
如果你的文本只包含普通字符,你可以只使用str.decode
:
>>> a=u"text"
>>> a
u'text'
>>> a.encode() # equivalent to a.encode("utf-8")
'text'
并对列表中的每个元素执行此操作:
>>> l=[(u'Test test', ), (u'i', ), (u'k', ), (u'b', ), (u'e', ), (u'n', ), (u'j', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', ), (u'x', )]
>>> [(i[0].encode(),) for i in l]
[('Test test',), ('i',), ('k',), ('b',), ('e',), ('n',), ('j',), ('x',), ('x',), ('x',), ('x',), ('x',), ('x',), ('x',)]
utf-8
是一个不错的选择,因为它涵盖了几乎所有内容,但您可以按ascii
解码:
>>> a.encode("ascii")
'text'
如果文本包含非 ASCII 字符,则可能需要提供errors
选项进行编码,例如 ignore
(跳过无法处理的字符):
>>> a=u"The number is π"
>>> a
u'The number is u03c0'
>>> a.encode("ascii")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'u03c0' in position 14: ordinal not in range(128)
>>> a.encode("ascii", errors="ignore")
'The number is '
有关更多示例,请参阅 Python 2 Unicode HOWTO。