0

I do not want to print of all of the contents of the file at once because I will be collecting the words later. This is my code here:

file = open('file analysis.txt', 'r')
chars = ''
for line in file:
    for ch in file:
        chars = chars + ch
        if ch == ' ':
            print(chars)
file.close()

Will be at school for a few hours so may not respond to questions straight away.

Qwertykey
  • 21
  • 5
  • 3
    You may have meant `for ch in line` instead of `for ch in file`. Also in case of last word in a line `ch` will be `\n'` instead of space – kuro Jun 22 '17 at 06:30
  • 1
    Also, did you perhaps meant to reset `chars` back to `''` again after printing? – Martijn Pieters Jun 22 '17 at 06:36

2 Answers2

0

You got it almost right, if you want to iterate over the lines of the file and then over each character in a line, change your second for to for ch in line:.

As kuro mentions in the comments, you are currently printing the read words only when you encounter a space character, not on newlines. The way your print works right now is printing all read words after each space, so a sample output might look like this:

Lorem 
Lorem ipsum 
Lorem ipsum dolor 
Lorem ipsum dolor sit 
Lorem ipsum dolor sit amet, 
Lorem ipsum dolor sit amet, consetetur 
Lorem ipsum dolor sit amet, consetetur sadipscing 

If you only mean to print each new word you encounter, you probably want to reset chars = '' after printing. To suppress newlines after each word, you can use print(chars, end='').

It is also safer to open files inside a with statement, that way closing the file will be handled for you.

Then your code could look like this:

with open('file analysis.txt', 'r') as file:
    chars = ''
    for line in file:
        for ch in line:
            chars = chars + ch
            if ch == ' ':
                print(chars, end='')
                chars = ''

This would give the following output:

Lorem ipsum dolor sit amet, consetetur sadipscing

Notice that you print only on spaces, so when your last word is not followed by a space, it will be read but not printed.

Christian König
  • 3,437
  • 16
  • 28
0

A few things.

First, instead of using file.open() and file.close(), you should use

with open('file analysis.txt', 'r') as f:

which automatically closes the file for you. Additionally, you should use for ch in line: not for ch in file: Because you want to print each character in the line, not each line in the file twice. Also, you want to reset your chars variable to the empty string after printing each time, so you don't print the same word multiple times. Finally, python's print() function will by default create a newline after each print statement. You can avoid this by doing print(string, end='') which replaces the end of line character with the empty string.

Taking all this into account, your code them becomes:

with open('file analysis.txt', 'r') as f:
chars = ''
for line in f:
    for ch in line:
        chars += ch
        if ch == ' ':
            print(chars, end='')
            chars = ''

If you're interested in learning more about the print(string, end='') function, look up positional arguments vs keyword arguments For more about the with.. as statement, look here

rma
  • 1,853
  • 1
  • 22
  • 42