python读取文件的readlines设置参数无效

lee110 2018-05-16 04:24:12
一个文本文件里一共四行文字
中国
python
java
c++
如果用readlines()可以正常读入
但如果给readlines设置参数为1和2,只能读取第一行的数据,只有设置为3才能读取2行
请问,这是什么错误?谢谢
...全文
1452 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
海鸥John Lee 2018-09-05
  • 打赏
  • 举报
回复
Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
读取并返回流中的行列表。可以指定提示以控制读取的行数:如果所有行的总大小(以字节/字符表示)超过提示,将不再读取行数。
字节/字符 此处划重点
欢乐的小猪 2018-05-21
  • 打赏
  • 举报
回复
引用 2 楼 lee110 的回复:
不好意思,还没没搞懂,为什么1,2可以读取第一行,3-9才可以读取前两行,全部四行需要将参数设置为15呢?
字节数。读到你设定的字节数就不再往下读行了
八百度 2018-05-21
  • 打赏
  • 举报
回复
每一行后面有一个换行符,双数行都是空行,你自己for循环出来看一下就知道了,下面是实际打印出来的效果 1:中国 2: 3:python 4: 5:java 6: 7:c++ 8:
lee110 2018-05-17
  • 打赏
  • 举报
回复
不好意思,还没没搞懂,为什么1,2可以读取第一行,3-9才可以读取前两行,全部四行需要将参数设置为15呢?
欢乐的小猪 2018-05-16
  • 打赏
  • 举报
回复
readlines(hint=-1) Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
0
['中国\n', 'python\n', 'java\n', 'c++\n']
1
['中国\n']
2
['中国\n']
3
['中国\n', 'python\n']
4
['中国\n', 'python\n']
5
['中国\n', 'python\n']
6
['中国\n', 'python\n']
7
['中国\n', 'python\n']
8
['中国\n', 'python\n']
9
['中国\n', 'python\n']
10
['中国\n', 'python\n', 'java\n']
11
['中国\n', 'python\n', 'java\n']
12
['中国\n', 'python\n', 'java\n']
13
['中国\n', 'python\n', 'java\n']
14
['中国\n', 'python\n', 'java\n']
15
['中国\n', 'python\n', 'java\n', 'c++\n']

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧