python中的编码格式

不同类型的数据转变为str

import MySQLdb
con=MySQLdb.connect(host = '172.100.7.219',user = 'hexin',passwd='sat1028&THS',port= 3306,db='dss',charset='utf8')
cur = con.cursor()
cur.execute("select channel_name,alarm_type,alarm_date,alarm_stat from adm_log_alarm where alarm_type >='1' and (channel_name like '%%兴议%%' or channel_name like 'XY%%') limit 1".decode("utf8"))
1L
alldata = cur.fetchall()
print alldata
(('XY-\xe5\x8c\x97\xe9\x80\x9a\xe9\x81\x93\xe8\xa5\xbf', 3, datetime.datetime(2019, 11, 7, 10, 16, 18), 1),)
  • 循环取值

for sql_info in alldata:
warn_id = sql_info[0].decode(“utf8”).encode(“gbk”).strip()
if sql_info[1] == 3:
warn_be = u’爱你一万年’
else:
warn_be = u’爱你1万年’
warn_date = sql_info[2].strftime(’%Y-%m-%d %H:%M:%S’).encode(“gbk”)

  • 各个值之前类型和值
    类型
type(sql_info[0])
<type 'str'>
type(sql_info[1])
<type 'int'>
type(sql_info[2])
<type 'datetime.datetime'>

print sql_info[0]
XY-北通道西
print sql_info[1]
3
print sql_info[2]
2019-11-07 10:16:18

各个值之后类型和值(标准格式为utf8,转为gbk后为乱码)
类型

 type(warn_id)
<type 'str'>
type(warn_be)
<type 'str'>
type(warn_date)
<type 'str'>

print warn_id
XY-▒▒ͨ▒▒▒▒
print warn_be
▒▒▒▒һ▒▒▒▒
print warn_date
2019-11-07 10:16:18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值