读取数据 为什么在python 2.7可以运行 在Python3.3不可以运行
123.txt的文档如下:this,is,good,for,yougive,me,five,goodit,is,good,girl代码如下:#!/usr/bin/pyth...
123.txt的文档如下:
this,is,good,for,you
give,me,five,good
it,is,good,girl
代码如下:
#!/usr/bin/python
#-*- coding: cp936 -*-
for i in open('123.txt','rb'): #遍历文档
k=i.split(',')
print k
用Python2.7的运行结果是:
['this', 'is', 'good', 'for', 'you\r\n']
['give', 'me', 'five', 'good\r\n']
['it', 'is', 'good', 'girl']
用Python3.3运行出错(已经把“print k”改成“print (k)",还是出错):
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\read.py", line 6, in <module>
fl=i.split(',')
TypeError: Type str doesn't support the buffer API
请教大神,是不是Python 2.7与Python3.3在读取文档时对于分行读取有不同的定义?
出错显示为:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\find.py", line 4, in <module>
k=i.split(',')
TypeError: Type str doesn't support the buffer API 展开
this,is,good,for,you
give,me,five,good
it,is,good,girl
代码如下:
#!/usr/bin/python
#-*- coding: cp936 -*-
for i in open('123.txt','rb'): #遍历文档
k=i.split(',')
print k
用Python2.7的运行结果是:
['this', 'is', 'good', 'for', 'you\r\n']
['give', 'me', 'five', 'good\r\n']
['it', 'is', 'good', 'girl']
用Python3.3运行出错(已经把“print k”改成“print (k)",还是出错):
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\read.py", line 6, in <module>
fl=i.split(',')
TypeError: Type str doesn't support the buffer API
请教大神,是不是Python 2.7与Python3.3在读取文档时对于分行读取有不同的定义?
出错显示为:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\find.py", line 4, in <module>
k=i.split(',')
TypeError: Type str doesn't support the buffer API 展开
1个回答
展开全部
for i in open('123.txt','rb'):
改成
for i in open('123.txt','r'):
试试,b是二进制,无法split
改成
for i in open('123.txt','r'):
试试,b是二进制,无法split
更多追问追答
追问
改成for i in open('123.txt','r'):也不可以,问题在于我用Python2.7是可以正常运行出答案的,用Python3.3就显示出错,所以想问问,这两个版本的编程语句是不是在读取数据的时候存在差异,那么如果我想用Python3.3来运行,应该怎么改呢?
追答
for i in open('123.txt','rb').readlines()
和b没关系。这应该没问题了。
sorry,我对python3不熟悉。不知道为什么3.3会报错,看一下官方文档,python 3.4版本的open不允许迭代的。
Changed in version 3.4: The file is now non-inheritable.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询