简体   繁体   中英

Python .readline()

I am working through Learn Python the Hard Way using Power Shell and NotePad++.

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. 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.

The script I wrote to execute (called new 1.py ) goes like this:

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:

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)? And why is there a white box in front of the capital L in the output?

The method readline() reads one entire line from the file. A trailing newline character is kept in the string. You don't have to put readline twice. If you will put that then you will get the result like line2,line4,line6 and empty string for the arg3 passed.

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. You can see them with repr() function:

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:

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)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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