简体   繁体   English

Python .readline()

[英]Python .readline()

I am working through Learn Python the Hard Way using Power Shell and NotePad++. 我正在使用Power Shell和NotePad ++学习使用艰难的方法学习Python。

I have gotten to the part where I am using .readline() and I noticed the first character of the first argument in my function gets deleted or overwritten with a space. 我已经到了我正在使用.readline() ,我注意到我的函数中第一个参数的第一个字符被删除或用空格覆盖。 I know there already is a question that sort of seems to answer this question ( Python .readline() ) but as I am completely new to Python and Powershell I have no idea how to fiddle around and change settings in either of the two. 我知道已经有一个问题似乎回答了这个问题( Python .readline() ),但由于我对Python和Powershell完全不熟悉 ,我不知道如何摆弄并更改两者中的任何一个。

The script I wrote to execute (called new 1.py ) goes like this: 我写的执行脚本(称为new 1.py )是这样的:

from sys import argv
script, input_filename = argv

def foo (arg1,arg2,arg3):
    print arg1,arg2,arg3.readline()

def bar (arg1, arg2, arg3):
    print arg2,arg1,arg3.readline()

open_input_file=open(input_filename)

foo ("Text1",1,open_input_file)
foo ("Text2",2,open_input_file)
bar ("Text3",3,open_input_file)
bar ("Text4","4",open_input_file)

With a test1.py file containing the text: 使用包含文本的test1.py文件:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

My output was as follows: 我的输出如下:

$ python "new 1.py" test1.py
ext1 1 ☐ Line 1
ext2 2  Line 2
  Text3  Line 3
  Text4  Line 4

The output that I expected is: 我期望的输出是:

$ python "new 1.py" test1.py
Text1 1  Line 1
Text2 2  Line 2
3 Text3  Line 3
4 Text4  Line 4

Could someone please explain how to get .readline() to read the line without erasing or overwriting the first character (with a space)? 有人可以解释如何让.readline()读取该行而不删除或覆盖第一个字符(带空格)? And why is there a white box in front of the capital L in the output? 为什么输出中的首都L前面有一个白盒子?

The method readline() reads one entire line from the file. readline()方法从文件中读取整行。 A trailing newline character is kept in the string. 尾随换行符保留在字符串中。 You don't have to put readline twice. 您不必将readline放两次。 If you will put that then you will get the result like line2,line4,line6 and empty string for the arg3 passed. 如果你将它放在那里你将获得结果,如line2,line4,line6和空字符串为arg3传递。

try the following code for the method defined. 为定义的方法尝试以下代码。

def foo (arg1,arg2,arg3):
    print arg1,arg2,arg3.readline().rstrip("\n")

def bar (arg1, arg2, arg3):
    print arg2,arg1,arg3.readline().rstrip("\n")

The readline() output always contains end-of-line character at the end. readline()输出始终包含末尾的行尾字符。 You can see them with repr() function: 你可以用repr()函数看到它们:

print repr(bl.readline())

In most cases, you want to strip them: 在大多数情况下,您想要剥离它们:

bl.readline().rstrip('\r\n')

If you do not care about regular spaces at the beginning/end of the line, you can simplify this to: 如果您不关心行的开头/结尾的常规空格,可以将其简化为:

bl.readline().strip()

Upon advise of theamk, jonrsharpe, Martijn Pieters and Nitu I tried the following script: 根据theamk,jonrsharpe,Martijn Pieters和Nitu的建议我尝试了以下脚本:

from sys import argv
script, input_filename = argv

def foo (arg1,arg2,arg3):
    print arg1,arg2,arg3.readline().strip("\r\n")

def bar (arg1, arg2, arg3):
    print arg2,arg1,repr(arg3.readline().strip("\r\n"))

open_input_file=open(input_filename)

foo ("Text1",1,open_input_file)
foo ("Text2",2,open_input_file)
bar ("Text3",3,open_input_file)
bar ("Text4","4",open_input_file)

And wrote a new test2.py file containing the same text as in the 'test1.py' file, but this time I typed all of the six lines by hand (instead of coppy pasting the text from the previous document) 并编写了一个新的test2.py文件,其中包含与'test1.py'文件中相同的文本,但这次我手动输入了所有六行(而不是粘贴上一文档中的文本)

My output is now as follows: 我的输出现在如下:

$ python "new 1.py" test2.py
Text1 1  Line 1
Text2 2  Line 2
3 Text3  ´Line 3´
4 Text4  ´Line 4´

Which is exactly the output that I expected for this script. 这正是我对此脚本的预期输出。 Thank you all very much for helping me with figuring this out! 非常感谢大家帮助我解决这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM