永发信息网

读取数据 为什么在python 2.7可以运行 在Python3.3不可以运行

答案:2  悬赏:60  手机版
解决时间 2021-12-29 04:52
  • 提问者网友:城市野鹿
  • 2021-12-28 09:59
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
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
k=i.split(',')
TypeError: Type str doesn't support the buffer API
最佳答案
  • 五星知识达人网友:未来江山和你
  • 2021-12-28 10:20
for i in open('123.txt','rb'):

改成
for i in open('123.txt','r'):

试试,b是二进制,无法split
全部回答
  • 1楼网友:我住北渡口
  • 2021-12-28 10:38
phi_file = sys.argv[1] 这个取决你执行脚本时的调用方式,需要把你执行脚本时的命令贴出来 如:python setup.py install
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯