Python readline 没有给出预期的输出

意图过滤器
from sys import argv

script,inputfile=argv

def print_all(file):
  print(file.read())

def rewind(file):
  file.seek(0)

def print_line(line,file):
  print(line,file.readline())

currentFile=open(inputfile)

print("Let's print the first line: \n")

print_all(currentFile)

print("Rewind")
rewind(currentFile)

currentLine=1

print_line(currentLine,currentFile)

currentLine+=1

print_line(currentLine,currentFile)


currentLine+=1


print_line(currentLine,currentFile)

我有这段代码,这行得通,但我不明白的是,当我将打印行函数中的打印语句重写为 print(file.readline(line)) 时,我得到了一个意外的输出。我正在使用 python 3.6

正确的输出

This is line 1
This is line 2
This is line 3
Rewind
This is line 1

This is line 2

This is line 3

错误的输出

This is line 1
This is line 2
This is line 3
Rewind
T
hi
s i

为什么会这样?

恶劣的加格

这是因为 的函数定义file.readline()

readline(...)
    readline([size]) -> next line from the file, as a string.

    Retain newline.  A non-negative size argument limits the maximum
    number of bytes to return (an incomplete line may be returned then).
    Return an empty string at EOF.

因此,当您将行号作为参数传递时,您实际上是在告诉每个currentLine+=1.

如果你只是想逐行打印内容,你可以参考这个,

def print_file_line_by_line(currentFile):
    for line in currentFile:
        print line.strip('\n')

或者这也有效

def print_file_line(currentLine, currentFile):
    try:
        print currentFile.read().split('\n')[currentLine-1]
    except IndexError as e:
        print str(currentLine)+' is greater than number of lines in file'
        print ''+str(e)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python strip函数没有给出预期的输出

来自分类Dev

python中的dir(sys)没有给出预期的输出

来自分类Dev

试图在python中做一个余弦规则公式,但是没有给出预期的输出

来自分类Dev

Python Selenium没有给出预期的结果

来自分类Dev

Python .readline()

来自分类Dev

python socket readline没有socket.makefile()

来自分类Dev

Python OOP Monty Hall没有给出预期的结果

来自分类Dev

Python seek()后跟readline()给出空字符串

来自分类Dev

Python 2子进程:无法从readline获取输出

来自分类Dev

Python数组程序没有给出正确的输出

来自分类Dev

readline()方法给出错误:AttributeError:'tuple'对象没有属性'readline'

来自分类Dev

带有多个换行符的Python文件readline问题

来自分类Dev

python中的read()和readline()有什么区别?

来自分类Dev

差异,.diff()没有给出预期的输出

来自分类Dev

该程序没有给出预期的输出

来自分类Dev

pytorch 没有给出预期的输出

来自分类Dev

Pytesseract 没有给出预期的输出

来自分类Dev

pthread 没有给出预期的输出

来自分类Dev

Python readline和readlines行为

来自分类Dev

Python stdout.readline()冻结

来自分类Dev

Python readline和readlines行为

来自分类Dev

Python stdout.readline()冻结

来自分类Dev

python的棉花糖给出ValueError:没有足够的值要解压(预期2,得到1)

来自分类Dev

在python中使用正则表达式的模式匹配没有给出预期的操作

来自分类Dev

为什么在使用readline之前在python循环中文件中的行没有遍历所有行?

来自分类Dev

使用readline防止输出回车

来自分类Dev

BufferedReader.readline()阻止输出

来自分类Dev

使用readline防止输出回车

来自分类Dev

BufferedReader在readLine中没有阻塞

Related 相关文章

  1. 1

    python strip函数没有给出预期的输出

  2. 2

    python中的dir(sys)没有给出预期的输出

  3. 3

    试图在python中做一个余弦规则公式,但是没有给出预期的输出

  4. 4

    Python Selenium没有给出预期的结果

  5. 5

    Python .readline()

  6. 6

    python socket readline没有socket.makefile()

  7. 7

    Python OOP Monty Hall没有给出预期的结果

  8. 8

    Python seek()后跟readline()给出空字符串

  9. 9

    Python 2子进程:无法从readline获取输出

  10. 10

    Python数组程序没有给出正确的输出

  11. 11

    readline()方法给出错误:AttributeError:'tuple'对象没有属性'readline'

  12. 12

    带有多个换行符的Python文件readline问题

  13. 13

    python中的read()和readline()有什么区别?

  14. 14

    差异,.diff()没有给出预期的输出

  15. 15

    该程序没有给出预期的输出

  16. 16

    pytorch 没有给出预期的输出

  17. 17

    Pytesseract 没有给出预期的输出

  18. 18

    pthread 没有给出预期的输出

  19. 19

    Python readline和readlines行为

  20. 20

    Python stdout.readline()冻结

  21. 21

    Python readline和readlines行为

  22. 22

    Python stdout.readline()冻结

  23. 23

    python的棉花糖给出ValueError:没有足够的值要解压(预期2,得到1)

  24. 24

    在python中使用正则表达式的模式匹配没有给出预期的操作

  25. 25

    为什么在使用readline之前在python循环中文件中的行没有遍历所有行?

  26. 26

    使用readline防止输出回车

  27. 27

    BufferedReader.readline()阻止输出

  28. 28

    使用readline防止输出回车

  29. 29

    BufferedReader在readLine中没有阻塞

热门标签

归档