-2

Basicaly, what I have to do is to open a certain text file and output the content of it in a reversed order like this "Hello world" ---> "world Hello". I have written some code so far but I'm not even sure I'm on the right direction, please help me to understand what should I do next and if what I already wrote is a good start or did I totally miss it

the code is:
t = open("text.txt", "r")
n = (t.readline)
line = t.readline()
word = line.split()
a = []
a.append(word)
2
  • Do you want the content of each line reversed, but the lines in their original order, or the individual lines reversed and in reversed order? May 20, 2015 at 21:06
  • 7
    The most important thing you can do when learning a new language is approach these things like a puzzle. Is your code reading that file correctly? Try printing "line" to be sure. Knowing how Python lists work, how should you fill "a"? Answering these questions yourself will help you more in becoming a competent coder than StackExchange will :)
    – manglano
    May 20, 2015 at 21:06

3 Answers 3

0

If you're trying to reverse each line individually then,

with open('text.txt', 'r') as f:
    for line in f:
        words = line.split()
        print(words.reverse())
2
  • No I wanted everything to be reversed and not each line separatelly
    – likurges
    May 20, 2015 at 21:14
  • and sadly when i try this out I get an error message saying "words = f.split() AtributeError: '_io.TextIOWrapper' object has no attribute 'split'"
    – likurges
    May 20, 2015 at 21:20
-1

If you're on Python 3:

Input file:

hello world!
more text

Code:

with open('text.txt', 'r') as f:
    print(*f.read().split()[::-1])

read() reads the entire contents of the file into one big string. split() splits a string into a whitespace-separated list. The * operator unpacks the sequence, sending it to print(). print(*[1,'a',3]) produces the same results as print(1,'a',3). [::-1] reverses the list. So, it reads the file, splits it by word, reverses that, and sends each word to print().

Output:

text more world! hello
6
  • oh thanks, that is what I was looking for. I just have to ask if it's not too much what does the * in print(*f.read().split[::-1]) do and what about the [::-1]. I guess that reverses the words that have bens split but thats's a very vague explanation sadly. thanks again for your help.
    – likurges
    May 20, 2015 at 21:12
  • @likurges * flattens the list back to a string ;) nice and easy solution. only downside is that it eliminates any mulitple spaces and tabs.
    – nullptr
    May 20, 2015 at 21:14
  • 3
    @nullptr * does nothing of the sort. It unpacks the argument. In this case, the list returned by f.read().split()[::-1] is "turned into" individual arguments for the print function: print('text', 'more', 'world!', 'hello')
    – That1Guy
    May 20, 2015 at 21:17
  • 1
    @nullptr Regardless of any invented definition of the word "flatten" you may have, * does not have anything to do with anything becoming a string.
    – That1Guy
    May 20, 2015 at 21:21
  • 1
    I don't understand why this keeps getting downvoted. What is wrong with it? May 20, 2015 at 23:42
-1

If you are on python 2.7: I tried to simplify my code as much as possible:

var = open('text.txt', 'r')
a = var.readlines()

for i in a:
    a = i.split()
    b =  a[::-1] # this is the pythonic way to reverse the string
    final_string = ''
    for i in b:
        final_string += i + ' '
    print final_string

do let me know if you have any questions

16
  • You can iterate directly over var instead of calling readlines() and iterating over that: for i in var:. a is a list, not a string. Instead of concatenating a space and each word of the reversed line, you can just join() them: print ' '.join(b). May 20, 2015 at 21:22
  • I'm on python 3.4 but I guess with a bit fo tricking that should work
    – likurges
    May 20, 2015 at 21:23
  • @TigerhawkT3 Thanks for the suggestion; I wanted to show every steps in a very basic way! thanks again!
    – ap2051
    May 20, 2015 at 21:24
  • @likurges I think this should on work on python 3.4 too. I encourage you to try it! And do let me know if you are confused on any steps or if error pops out on python 3.4 it should be fine though...
    – ap2051
    May 20, 2015 at 21:26
  • All you have to do for this to work on Python 3 is to turn the print statement into a function by putting parentheses around what you're printing. Also, this prints the lines in order, with their words reversed. May 20, 2015 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.