[Tutor] Closing triple quotation marks.
Lisi
lisi.reisz at gmail.com
Sat Jun 18 15:50:29 CEST 2011
On Saturday 18 June 2011 13:37:38 Steven D'Aprano wrote:
> Lisi wrote:
> [...]
>
> > Fair enough. the closing quotation marks are not there.
> >
> > But when they _are_ there, i.e. when that stanza reads:
> >
> > target.write """
> > line1\nline2\nline3\n
> > """
>
> This is not the problem, but I just thought I'd mention that it's a bit
> silly to go to the trouble of using newline escape characters inside a
> triple-quoted string! You can do it if you want, but this would be more
> naturally written as:
>
> """
> line 1
> line 2
> line 3
> """
>
> Now, on to your actual error:
> > I get:
> >
> > lisi at Tux:~/Python/LearnPythonTheHardWay$ python extra-credit_16a.py
> > learning.txt
> > File "extra-credit_16a.py", line 38
> > """
> > ^
> > SyntaxError: invalid syntax
>
> This has nothing to do with the triple quote marks. Simplify the code by
> shrinking the text inside the quotes to a single line, and you get:
>
> target.write "..."
>
>
> and you will get the same SyntaxError. Can you see the problem? No
> brackets! You need to include parentheses to call the write method:
>
> target.write("...")
>
>
> Then you can expand the string to use a triple-quote:
>
> target.write("""
> line 1
> line 2
> line 3
> """)
>
> and all should be good.
Thanks very much, Steven. I did at some stage in the dim and distant past try
brackets, but I probably put them in the wrong place.
But I still can't write to the file.
If I do:
target.write(line1)
The value of the variable line1 is written to the file. But if I put the
three variables into the write command, what gets printed is the name of the
variables, not their values. I am clearly still doing something wrong. But I
can't see what. I have even tried to see whether ''' gave a different result
from """, but it doesn't. I have accepted KWrite's idea of what the white
space should be, and I have adapted it to all the variations I can think of.
At the moment it is:
target.write("""
line1
line2
line3
""")
I am beginning to feel paranoid! I simply can't see in what way that differs
from yours.
In case it is relevant, I am using Python 2.5.2 (r252:60911, Jan 24 2010,
14:53:14) [GCC 4.3.2] on Debian 5 with a 2.6.26 kernel.
I was trying not to move on until I had mastered everything in this section,
but I am beginning to think that that is foolish. I am getting myself more
and more bogged down, and it might be more sensible to pass on and come back
to it.
Again, thank you for your very prompt help.
I have, I hope, finally _fully_ taken in that many commands need (). And that
it is possible to get a new line just by giving a new line - without the
explicit instruction. I.e., hopefully, I have learnt something.
Lisi
More information about the Tutor
mailing list