northisland
V2EX  ›  问与答

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

  •  
  •   northisland · Mar 7, 2016 · 2083 views
    This topic created in 3925 days ago, the information mentioned may be changed or developed.
    # -*- 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 replies  •  2016-03-07 09:39:14 +08:00
    Sylv
    Sylv
    Mar 7, 2016❤️1
    1
    print xxx # 这是 Python 2 的 print 语法
    print(xxx) # 这是 Python 3 的 print 语法
    当你在 Python 2 下 from __future__ import print_function 后,就得改用 Python 3 的 print 语法了,否则就会报语法错误。
    所以你在这得:
    print(type(s2))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2001 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:21 · PVG 17:21 · LAX 01:21 · JFK 04:21
    ♥ Do have faith in what you're doing.