| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1238 人关注过本帖
标题:Python 3.1.1 标准库自己带错?
只看楼主 加入收藏
bluegene8210
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-1
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:2 
Python 3.1.1 标准库自己带错?
我下了 Python 3.1.1 的 Windows 安装包:http://www.
,装好以后用它来运行一个测试脚本,出了如下的问题:

Traceback (most recent call last):
  File "D:\resource\programming\Python\MyPythonScripts\test.py", line 132, in vimcomplete
    cmpl.evalsource(filestr, lineidx)    # 改了
  File "D:\resource\programming\Python\MyPythonScripts\test.py", line 170, in evalsource
    sc = self.parser.parse(text, line)
  File "D:\resource\programming\Python\MyPythonScripts\test.py", line 567, in parse
    self.gen = tokenize.tokenize(buf.readline)
  File "D:\Program Files\Python31\lib\tokenize.py", line 369, in tokenize
    encoding, consumed = detect_encoding(readline)
  File "D:\Program Files\Python31\lib\tokenize.py", line 329, in detect_encoding
    if first.startswith(BOM_UTF8):
TypeError: Can't convert 'bytes' object to str implicitly

看起来问题出在标准库 tokenize.py 内部,329行那里。startswith() 函数的参数类型不对。查阅同时安装的帮助文档,里面确实写了,BOM_UTF8 是 bytes 类型的常数,可是 startswith() 函数要求参数是 str 类型。难道是标准库自己带错?
搜索更多相关主题的帖子: Python 
2010-03-02 22:35
好学
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:622
专家分:318
注 册:2004-5-4
收藏
得分:20 
别装在Program Files里面,这个文件夹路径有空格,Python不允许安装路径里面有空格,建议直接装在C盘

C:\Python31

2010-03-02 23:53
bluegene8210
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-3-1
收藏
得分:0 
回复 楼主 bluegene8210
原因找到了,标准库没错(汗 ...),是用得不对。

正确用法:
--------------------------------------------------------------------------------
import tokenize

testbuffer= open('test.py', mode='rb')    # !! 一定要有 'b'
tokengenerator= tokenize.tokenize(testbuffer.readline)
--------------------------------------------------------------------------------
这是文档里的解释:

tokenize.tokenize(readline)
    The tokenize() generator requires one argument, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects (see section File Objects). Each call to the function should return one line of input as bytes.

2010-03-06 15:49
快速回复:Python 3.1.1 标准库自己带错?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017922 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved