| | |
Reverse Data File
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2007
Posts: 1
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
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.
The fourth line is the one that does the job. Here it is, in a more friendly code block:
python Syntax (Toggle Plain Text)
inp = open("somefile", "r") out = open("anotherfile", "w") out.write("\n".join(" ".join(reversed(i.rstrip().split())) for i in reversed(inp.readlines()))) inp.close() 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)
#we will produce each line one by one and store here result = [] for i in reversed(inp.readlines()): # get the lines in reverse fashion i = i.rstrip() # strip trailing newline # split the data into a list, then reverse the order and join back with one space separating data string = " ".join(reversed(i.split())) #the line is correct, add it to the list result.append(string) #join all of the lines, inserting a newline in-between write_str = "\n".join(result) #write out.write(write_str)
Last edited by ffao; Jun 28th, 2007 at 10:18 am. Reason: use rstrip()
![]() |
Similar Threads
- Please help with data file and array (C++)
- Help creating a data file (C)
- input data from a file into an Array (C)
- Reading from external data file (C++)
- data file help (C)
Other Threads in the Python Forum
- Previous Thread: python program with italian explanation ,
- Next Thread: Python noob needs random help
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv beginner change command csv def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric obexftp output parameters parsing path phonebook port prime programming projects py2exe pygame pygtk pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning smtp software sprite statictext stderr string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip windows wordgame wxpython





