[英]Python unicode and list
I am currently python learning. 我目前正在学习python。 I'm working on an email script. 我正在处理电子邮件脚本。 Sending the mail goes well, I just want to add data to the mail which stands in my database. 发送邮件进展顺利,我只想向数据库中的邮件中添加数据。 This he does, he just gives it as one long list again. 他这样做,他只是再次列出一长串。 like: 喜欢:
[(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', )]
He also gives the string as: uTEXT instead of TEXT 他还将字符串指定为:uTEXT而不是TEXT
On several sites I've already looked at how to encode etc, I just do not get that working. 在几个站点上,我已经研究了如何编码等,但是我只是没有使它工作。 How do I get this working? 我该如何工作? Probably it's something very simple for most of you. 对于大多数人来说,这可能非常简单。
I would also like to display the values from my list below and solve. 我还想显示下面列表中的值并求解。 Without the list of characters -> (['TEXT']) 没有字符列表->(['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 = "\r\n".join(headers)
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(login, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + message)
session.quit()
The result of my query and rows is this, I will see this in my mail: 我的查询和行的结果是这样,我将在邮件中看到以下内容:
[(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've tried things like: 我已经尝试过类似的事情:
message = "text" + str(rows.encode('ascii') + "text
or: 要么:
[(rows[0].encode("utf-8"),) for rows in row]:
print rows
The result of my query and rows is this, I will see this in my mail: 我的查询和行的结果是这样,我将在邮件中看到以下内容:
[(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've tried things like: 我已经尝试过类似的事情:
message = "text" + str(rows.encode('ascii') + "text
or: 要么:
[(rows[0].encode("utf-8"),) for rows in row]:
print rows
For all those things I will get an error. 对于所有这些事情,我都会得到一个错误。 I think my way of convert data is not good. 我认为我转换数据的方式不好。 Or that I do not at the right time applicable. 还是我在适当的时候不适用。
If your text only contains normal characters, you can just use str.decode
: 如果您的文本仅包含普通字符,则可以使用str.decode
:
>>> a=u"text"
>>> a
u'text'
>>> a.encode() # equivalent to a.encode("utf-8")
'text'
And do that for each element in your list: 并对列表中的每个元素执行此操作:
>>> 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
is a good option, as it covers pretty much everything, but you can decode as ascii
instead: utf-8
是一个不错的选择,因为它涵盖了几乎所有内容,但是您可以将ascii
解码为:
>>> a.encode("ascii")
'text'
If your text contains non-ascii characters, you may have to give an errors
option to encode, such as ignore
(which skips unhandleable characters): 如果您的文本包含非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 '
See the Python 2 Unicode HOWTO for more examples. 有关更多示例,请参见Python 2 Unicode HOWTO 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.