944,188 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1131
  • Python RSS
Jun 28th, 2007
0

Reverse Data File

Expand Post »
I have a delimited text file with some data.
I need to revert the data in this way:
1) the last line will be the first, and son on (invert all the lines)
2) each line must have a reverted data (last data item will be the first and so on)

Example:
file IN:
100 200 230
238 345 333
234 455 248

file OUT
248 455 234
333 345 238
230 200 100

can you help me ?

Thanks in advance

cameyo
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cameyo is offline Offline
1 posts
since Jun 2007
Jun 28th, 2007
0

Re: Reverse Data File

First, I have to tell you that the data will be separated by one space only, regardless of how many there were in the original file. This is a simple code which has no concerns whatsoever with memory/speed, as it keeps the whole file in memory.

python Syntax (Toggle Plain Text)
  1. inp = open("somefile", "r")
  2. out = open("anotherfile", "w")
  3.  
  4. out.write("\n".join(" ".join(reversed(i.rstrip().split())) for i in reversed(inp.readlines())))
  5.  
  6. inp.close()
  7. out.close()

The fourth line is the one that does the job. Here it is, in a more friendly code block:

python Syntax (Toggle Plain Text)
  1. #we will produce each line one by one and store here
  2. result = []
  3.  
  4. for i in reversed(inp.readlines()): # get the lines in reverse fashion
  5. i = i.rstrip() # strip trailing newline
  6. # split the data into a list, then reverse the order and join back with one space separating data
  7. string = " ".join(reversed(i.split()))
  8. #the line is correct, add it to the list
  9. result.append(string)
  10.  
  11. #join all of the lines, inserting a newline in-between
  12. write_str = "\n".join(result)
  13.  
  14. #write
  15. out.write(write_str)
Last edited by ffao; Jun 28th, 2007 at 10:18 am. Reason: use rstrip()
Reputation Points: 20
Solved Threads: 5
Newbie Poster
ffao is offline Offline
15 posts
since May 2007
Jun 28th, 2007
0

Re: Reverse Data File

Hi Cameyo,

If you wish you can also go through this thread, which is recently discussed, which is for your further learning.

kath.
Last edited by katharnakh; Jun 28th, 2007 at 10:40 am.
Reputation Points: 19
Solved Threads: 34
Posting Whiz in Training
katharnakh is offline Offline
237 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python program with italian explanation ,
Next Thread in Python Forum Timeline: Python noob needs random help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC