V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
northisland
V2EX  ›  问与答

为什么这段 python 代码,有个变量不能访问,只能 print()

  •  
  •   northisland · 2016-03-07 09:19:42 +08:00 · 1602 次点击
    这是一个创建于 2970 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- encoding: utf-8 -*-
    from __future__ import print_function, unicode_literals
    import json
    import requests

    CLASSIFY_URL = 'http://api.bosonnlp.com/classify/analysis'
    s = ['17个新手常见Python运行时错误- 开源中国社区']
    data = json.dumps(s)
    headers = {'X-Token': 'ETqTqRl7.5146.8PzV6EsdISOY'}
    resp = requests.post(CLASSIFY_URL, headers=headers, data=data.encode('utf-8'))
    s2=resp.text.decode('utf-8')

    #print type(s2) #??? 为什么我不能查看s2,但可以用print()打印s2的结果,真不明白了
    print(s2)


    就是这个s2变量。不能用type查看,
    1 条回复    2016-03-07 09:39:14 +08:00
    Sylv
        1
    Sylv  
       2016-03-07 09:39:14 +08:00   ❤️ 1
    print xxx # 这是 Python 2 的 print 语法
    print(xxx) # 这是 Python 3 的 print 语法
    当你在 Python 2 下 from __future__ import print_function 后,就得改用 Python 3 的 print 语法了,否则就会报语法错误。
    所以你在这得:
    print(type(s2))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5032 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:11 · PVG 09:11 · LAX 18:11 · JFK 21:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.