3

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?

  • 3
    Try `.readline().strip()` to remove any pesky whitespace characters that may be affecting the output. Also, please read [the style guide](http://www.python.org/dev/peps/pep-0008/) and format your code accordingly (e.g. more sensible function/parameter names would help hugely). – jonrsharpe Sep 04 '15 at 14:36
  • 2
    Use `repr(bl.readline())` to print a debug representation of the line. You have additional bytes in the file that are interpreted by your console or terminal as control characters. – Martijn Pieters Sep 04 '15 at 14:38
  • 6
    And you may want to think about better variable names; two letter variables are very hard to track. – Martijn Pieters Sep 04 '15 at 14:38
  • I'm pretty sure this is about the format of the `test1.py` file - which text editor are you using to save it? I can't remember which python uses, but it looks to me like you're trying to read `char`s from `wchar`s. `wchar` is a "character" which consumes twice the memory of char, and if you treat them incorrectly you'll end up with mumbo jumbo like you've listed. Try saving the file you are calling `open` on as different formats, and try saving it as .txt instead of .py. – XtrmJosh Sep 04 '15 at 14:38
  • You're not doing what you think you're doing. Build a string of your inputs, and print the string. Google for python string formatting – Brian Pendleton Sep 04 '15 at 14:38
  • Also, I'm not sure what this has to do with Powershell? – EBGreen Sep 04 '15 at 15:37
  • Removed PowerShell tag. Question has nothing to do with PowerShell. – Bill_Stewart Sep 04 '15 at 16:02
  • I'm impressed that a new member actually read some SO documentation before posting. Kudos. – msw Sep 04 '15 at 16:13

3 Answers3

2

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")
Nitu
  • 51
  • 6
1

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()
theamk
  • 1,420
  • 7
  • 14
  • I have tried to use either print repr(arg3.readline()) arg3.readline().rstrip('\r\n') or arg3.readline().strip() But none of them seem to work. – Douwe van der Leest Sep 07 '15 at 18:11
  • @DouwevanderLeest Please show the code, your input, your output and your expected output. Just to make sure we know what you mean when you say "it doesn't work". – Martin Thoma Sep 07 '15 at 18:18
0

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!