鱼C论坛

 找回密码
 立即注册
查看: 2926|回复: 6

从unicode 到 str

[复制链接]
发表于 2016-3-19 08:51:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
city_name = raw_input('请输入要查询的城市名称:').strip().decode(sys.stdin.encoding)
print type(city_name)
city_name.encode('utf-8')
print type(city_name)
为什么 我将city_name编码了 为什么 type 出来 还是 unicode 编码格式,求大神 解释下。

>>>
请输入要查询的城市名称:长沙
<type 'unicode'>
<type 'unicode'>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-3-19 08:53:51 | 显示全部楼层
utf8是Unicode的子集
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-19 09:07:11 | 显示全部楼层
hldh214 发表于 2016-3-19 08:53
utf8是Unicode的子集

s = u'长沙'
print type(s)
t = s.encode('utf-8')
print type(t)
那这个怎么可以?

请输入要查询的城市名称:长沙
<type 'unicode'>
<type 'str'>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-19 09:08:03 | 显示全部楼层
hldh214 发表于 2016-3-19 08:53
utf8是Unicode的子集

  我用的是 python 2.7
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-19 09:26:15 | 显示全部楼层

不是很懂Python2, 嘿嘿= =
  1. >>> s = u'我是Python3'
  2. >>> type(s)
  3. <class 'str'>
  4. >>> t = s.encode('utf-8')
  5. >>> type(t)
  6. <class 'bytes'>
  7. >>> t
  8. b'\xe6\x88\x91\xe6\x98\xafPython3'
  9. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-19 10:07:44 | 显示全部楼层
hldh214 发表于 2016-3-19 09:26
不是很懂Python2, 嘿嘿= =

python3 不用处理 就可以了 主要是python2  输入 识别不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-19 10:29:16 | 显示全部楼层
hldh214 发表于 2016-3-19 09:26
不是很懂Python2, 嘿嘿= =

python3 不用处理 就可以了 主要是python2  输入 识别不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-20 05:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表