Re: [Tutor] new to python

2017-07-26 Thread N6Ghost



On 7/25/2017 12:43 AM, Alan Gauld via Tutor wrote:

On 25/07/17 04:58, N6Ghost wrote:


this code works
f = open("C:/coderoot/python3/level1/inputfile.txt", 'r')
for line in f:
  for line in f:
  #print(line.rstrip())
  print(line)

f.close()
the out put skips the first line of the inputfile and puts a blank line
inbetween


I'm not sure why you have two for loops? Why did you do that?
Can you explain your thinking there?

Remove one of the for... lines.

Your code does this:


f = open("C:/coderoot/python3/level1/inputfile.txt", 'r')

open the file and assign it to 'f'


for line in f:

get the first line from f and assign it to 'line'


  for line in f: print(line)

get the next line from f and assign it to 'line'
This overwrites the value from the first for loop above.
The line is then printed.

The second loop then repeats for all of the remaining
lines in the file. At the end of the second for loop
control returns to the top for loop. But, since the file
is now empty, the top loop never gets any more values
from f, so it terminates.

The blank lines are caused by the fact that the lines
in the file end in a newline character and print() adds
a newline of its own. Either reinstate your rstrip()
call or stop print() adding a newline with

print(line, end='')

I'm also not sure why you posted two copies of
your code? I assume you only use one since otherwise
you would have told us that you got two lots of output?

HTH


final working code for this method:
# open meth 1
f = open("C:/coderoot/python3/level1/inputfile.txt", 'r')
for line in f:
#print(line.rstrip())
print(line.strip())
j = line
#print ("below new var used")
#print (j)

f.close()


thanks for the replys, not sure why i had the second for loop. removing 
that remove some of the oddball behavioral issues.


next going to look into using the with


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new to python

2017-07-25 Thread N6Ghost



On 7/23/2017 1:03 AM, Alan Gauld via Tutor wrote:

On 23/07/17 07:26, N6Ghost wrote:


f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in file:

Note that you have no variable called 'file'.
So this line doesn't make sense.


  for line in f:
  print(line.rstripe())

This bit will work if you omit the line above and
fix the indentation. (and remove the 'e' from strip()


  f.close()

This should be outside the loop, you don't want
to close the file after every line.

Finally, there is another way to do this which
is considered 'better'/more Pythonic:

with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f:
  for line in f:
  print(line.strip())

Notice with this construct the closing of the file is
handled for you.


any idea why that does not work?

When posting questions always include the full error text.
Although apparently cryptic it actually contains a lot of
useful detail which saves us from making guesses.




this code works
f = open("C:/coderoot/python3/level1/inputfile.txt", 'r')
for line in f:
for line in f:
#print(line.rstrip())
print(line)

f.close()f = open("C:/coderoot/python3/level1/inputfile.txt", 'r')
for line in f:
for line in f:
#print(line.rstrip())
print(line)

f.close()

the out put skips the first line of the inputfile and puts a blank line 
inbetween


inputfile is:
tom
jerry
make
windows
linux

-N6Ghost

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new to python

2017-07-24 Thread N6Ghost



On 7/23/2017 1:03 AM, Alan Gauld via Tutor wrote:

On 23/07/17 07:26, N6Ghost wrote:


f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in file:

Note that you have no variable called 'file'.
So this line doesn't make sense.


  for line in f:
  print(line.rstripe())

This bit will work if you omit the line above and
fix the indentation. (and remove the 'e' from strip()


  f.close()

This should be outside the loop, you don't want
to close the file after every line.

Finally, there is another way to do this which
is considered 'better'/more Pythonic:

with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f:
  for line in f:
  print(line.strip())

Notice with this construct the closing of the file is
handled for you.


any idea why that does not work?

When posting questions always include the full error text.
Although apparently cryptic it actually contains a lot of
useful detail which saves us from making guesses.



update code:
f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in f:
for line in f:
print(line.rstripe())

f.close()


C:\coderoot\python3\level1>python secondscript.py
Traceback (most recent call last):
  File "secondscript.py", line 5, in 
print(line.rstripe())
AttributeError: 'str' object has no attribute 'rstripe'




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] new to python

2017-07-23 Thread N6Ghost

C:\coderoot\python3\level1>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

/windows 10 x64

new to python, but not really new to programming.

going through the beginners docs, I am stuck on the file handling opening:


  my code so far:

f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in file:
for line in f:
print(line.rstripe())
f.close()

I want to try the different methods of opening files and reading them. 
and processing the data.  from there I will then start with writing to 
files.



any idea why that does not work?


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor