-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)
hivert
  • 10,579
  • 3
  • 31
  • 56
  • 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? – Scott Hunter May 20 '15 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 '15 at 21:06

3 Answers3

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())
junnytony
  • 3,455
  • 1
  • 22
  • 24
  • No I wanted everything to be reversed and not each line separatelly – likurges May 20 '15 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 '15 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
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 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 '15 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 '15 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 '15 at 21:17
  • @That1Guy what do you think "flatten" meant in this context... Giving unnecessary information. +1 to compensate – nullptr May 20 '15 at 21:20
  • 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 '15 at 21:21
  • 1
    I don't understand why this keeps getting downvoted. What is wrong with it? – TigerhawkT3 May 20 '15 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

ap2051
  • 149
  • 1
  • 8
  • 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)`. – TigerhawkT3 May 20 '15 at 21:22
  • I'm on python 3.4 but I guess with a bit fo tricking that should work – likurges May 20 '15 at 21:23
  • @TigerhawkT3 Thanks for the suggestion; I wanted to show every steps in a very basic way! thanks again! – ap2051 May 20 '15 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 '15 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. – TigerhawkT3 May 20 '15 at 21:26
  • @likurges did you try it accordingly whay TigerhawkT3 mentioned! Did it worked for you? – ap2051 May 20 '15 at 21:32
  • oh I see and it works, thanks a lot. Is there a way that would reverse the whole text and not line by line? – likurges May 20 '15 at 21:34
  • @likurge could you please elaborate your query; i mean could you please show an example of what you want so I would better understand the problem. Thanks – ap2051 May 20 '15 at 21:36
  • @likurges for this one i should borrow the code of TigerhawkT3 with open('text.txt', 'r') as f: print(*f.read().split()[::-1]) – ap2051 May 20 '15 at 21:42
  • sure, the original text is : "hello world how are you" and the given resoult should be something like this: "you are how world hello". Now I see that it won't let me make paragraphs so I will explain it in a different way. it must output the text like it was written in the opposite order meaning what is at the end goes at the top and vice versa(that wher there are multiple paragraphs) – likurges May 20 '15 at 21:43
  • @likurge i mean you could look the answer of TigerHawkT3 if you need it whole reversed! If that helps its okay; if not I could modify it into more simpler form so that you could better understand! – ap2051 May 20 '15 at 21:43
  • yes plese@AshimPaudel if you could that, it would be great – likurges May 20 '15 at 21:47
  • @likurge it is simple than you think! when you run my original code what you get is every line in its reversed order. So at the beginning of your code creat a variable( something like bigger_string) and set its value to empty string( '' ). and every time code generates the reverse order for line concatenate that linke to bigger_string. Finally follow the same approach for bigger_string. Do let me know if you are confused so I could figure out something else! – ap2051 May 20 '15 at 21:56
  • @likurge did you get it? or should I modify my original answer to the way you want it! If you find my answer useful, please help me keep encouraged by hitting the green check mark, so that I can improve my reputation. – ap2051 May 20 '15 at 23:46
  • actually I'm a bit confused now because I tried what you @ap2051 said but I didn't manage it to work. I know it's me who made a mistake but whatever I try I just cant get it to work right – likurges May 21 '15 at 19:55
  • So, look at this code by @matt joiner. I am sure it wil help: At first step do as it is said on this answer http://stackoverflow.com/questions/2301789/read-a-file-in-reverse-order-using-python and after that follow my code! – ap2051 May 21 '15 at 20:52